【问题标题】:C# - Unable to return correct value to int function using for/if combinationC# - 无法使用 for/if 组合将正确的值返回给 int 函数
【发布时间】:2016-01-01 03:36:23
【问题描述】:

我有一个函数,它只需要返回当前显示在List<Bitmap> 中的图片框中的图像的索引,称为images。 它是一个名为displayImageIndex()int 函数,我最初尝试这样运行它:

public int displayImageIndex()
{
    //index to return to function
    int displayIndex = 0;

    //access all images in list
    for (int i = 0; i < imagePaths.Count; ++i)
    {
        //matches image in picturebox
        if (picboxImage.ImageLocation == imagePaths[i])
        {
            //get index of image
            displayIndex = imagePaths.IndexOf(imagePaths[i]);
        }
    }

    return displayIndex;
}

这个编译但总是返回 displayIndex 为 0,尽管在程序的其余部分正在工作时显然找到了图像。然后我尝试这样:

public int displayImageIndex()
{
    //value to return to function
    int displayIndex;

    //access all images in list
    for (int i = 0; i < imagePaths.Count; ++i)
    {
        //matches image in picturebox
        if (picboxImage.ImageLocation == imagePaths[i])
        {
            //get index of image
            displayIndex = imagePaths.IndexOf(imagePaths[i]);
            return displayIndex;
        }
    }
}

这给了我一个错误,告诉我“并非所有代码路径都返回一个值”。我试图通过在if 语句中添加else 来解决此问题,但这给了我一个警告,即循环可能包含无法访问的代码。

基本上,但是我尝试运行它时会遇到某种错误。 我不知道该怎么做,因为该函数绝对总是会找到一个值(picbox 上的图像总是来自images 列表中的一个),我只需要一种始终返回该值的有效方法,但我不知道如何将其放入函数中。

任何帮助或正确方向的观点都会很棒,提前谢谢!

标记

【问题讨论】:

  • 一些问题。为什么要使用预增量++i?你为什么使用函数indexofi 不是你的索引吗?
  • 您确定 ImageLocation 存在于 imagePaths 中吗?也许图像被移动了?另外,为什么要使用 IndexOf? “我”还不够好吗?

标签: c# list return


【解决方案1】:

您可以使用IndexOfToLower() 以这种方式查找项目:

public int DisplayImageIndex()
{
    return imagePaths.Select(x => x.ToLower()).ToList()
                     .IndexOf(picboxImage.ImageLocation.ToLower());
}

如果 imagePaths 不包含该路径,则该函数返回 -1。

【讨论】:

  • 如果 OP 有一个包含 100,000 张图像的列表会发生什么。您想为它们中的每一个分配一个小写字符串吗?这不会很好地扩展。
  • 如果 OP 有一个包含 100 000 或 1 000 000 或 10 000 000 个图像的列表,那么他无论如何都不应该使用这种方法,而是保存选定的索引或使用字典来保存路径-索引关系。
  • @YuvalItzchakov 从家里出来时,你默认不戴头盔。但是,如果您想骑摩托车,是的,您应该骑摩托车;)
  • @RezaAghaei 您考虑到 OP 要么知道这一点,要么他的数组大小很小。这两个都是假设。你至少应该告诉 OP,如果他试图扩展,这会产生很大的开销。
  • @georch 你可能是对的。但是不了解这一事实的人,也许是初学者,请将此作为不适合案例的建议。
【解决方案2】:

编译器不知道你的代码应该总是返回一些东西,并且“认为” if 永远不会被命中,在这种情况下,该方法不会返回任何东西。您可以通过 e 解决此问题。 G。如果没有找到,则抛出异常,如下所示:

for(...)
{
    //...
}
throw new Exception("The item you searched for isn't on the list!");

我不太清楚你为什么使用一种方法来获取你已经知道的东西的索引,我认为你可以替换这一行

displayIndex = imagePaths.IndexOf(imagePaths[i]);

这个:

displayIndex = i;

然后,您的整个函数应该与 IndexOf 本身具有相同的功能,因此您的整个代码应该缩减为如下所示:

return imagePaths.IndexOf(picboxImage.ImageLocation);

我猜您的错误是 picboxImage.ImageLocation 实际上并未包含在您的列表中。可能是格式不对。您应该尝试设置断点并通过将鼠标光标悬停在变量上来调查变量的内容。这是找出它们的确切内容的一种非常简单的方法。

【讨论】:

  • 请注意,IndexOutOfRangeException 在这种情况下没有任何意义
  • @codroipo 不仅如此,在这里使用它是有风险的,因为尝试在异常窗口中捕获 IndexOutOfRangeException 也会捕获这个并且可能会成为调试的噩梦。
  • 你是对的,抛出另一种类型的异常会更好。我相应地编辑了我的答案。
【解决方案3】:

好吧,显然找不到图像否则displayIndex会改变,所以你必须调试它,看看imagePaths是否真的包含@987654323 @。

  • 在方法中设置断点。
  • 使用 F11 进入。
  • 观察窗口中检查imagePaths,看看你的照片是否在那里。
  • 另请注意,string 比较区分大小写,因此您可以使用imagePaths[i].ToLower == picboxImage.ImageLocation.ToLower()picboxImage.ImageLocation.Equals(imagePaths[i], StringComparison.InvariantCultureIgnoreCase);

另外,你应该使用

displayIndex = i;

代替:

displayIndex = imagePaths.IndexOf(imagePaths[i]);

你已经有了索引!

对于并非所有代码路径都返回值,如果没有找到图像,您需要确保存在return 语句事件。只需在方法末尾添加一个默认的return 值,如下所示:

public int displayImageIndex()
{
    //access all images in list
    for (int i = 0; i < imagePaths.Count; ++i)
    {
        //matches image in picturebox
        if (picboxImage.ImageLocation == imagePaths[i])
        {
            //get index of image
            return i;
        }
    }

    return -1;
}

或者将其简化为如下Linq语句:

public int displayImageIndex()
{
    return imagePaths.FindIndex(x => x.Equals(picboxImage.ImageLocation, StringComparison.InvariantCultureIgnoreCase));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2011-01-28
    相关资源
    最近更新 更多