23

我正在尝试创建一个函数,但我收到一条错误消息。

public int[] genericSearch(int searchWidth, int startingRadius, int width, int height, Bitmap bitmap)
{
    //Generic function for finding the best path from a certain range
    if (startingRadius == -1)
        startingRadius = bitmap.Height() / 2;

在声明之前不能使用局部变量“startingRadius”。

位图变量也会出现同样的问题。通常在 c++ 中这种类型的声明会起作用;但是,我不确定为什么它在这里不起作用。

4

4 回答 4

66

在视觉工作室。有时当你再次声明一个变量时(第二次)。它会给出这个错误。例如,这有时会抛出您提到的异常:

 1.  int startingRadius = 0;
 2.  startingRadius = 5; <-- Exception thrown here.
 3.  
 4.  int startingRadius = 0;

显然这无论如何都是不正确的。因此删除第二个声明(第 4 行)将解决问题。

注意:您通常期望的例外是A local variable named 'startingRadius' is already defined in this scope. 但是由于某种原因,有时会显示您提到的异常。

于 2014-01-11T15:59:22.080 回答
4

您的方法缺少右括号,否则此代码可以在我的机器上编译...(也将高度更改为属性)

public int[] genericSearch(int searchWidth, int startingRadius, int width, int height,Bitmap bitmap)
         {
         //Generic function for finding the best path from a certain range
             if (startingRadius == -1)
                 startingRadius = bitmap.Height / 2;
         }
于 2013-07-30T00:18:46.237 回答
3

听起来你有一个放错地方}或拼写错误的变量名。如果没有看到完整的代码,我真的无法判断。

该错误消息基本上是告诉您您有一个尚未声明的局部变量,您正在尝试使用该变量。这表明if (startingRadius == 1)代码实际上位于与您声明的方法不同的方法中。

于 2013-07-30T00:20:03.477 回答
0

对我来说,它有助于完全删除局部变量并创建一个具有不同名称的新变量......

Originally:

string butterfly;

butterfly = "butterfly with error"; <-- gives error

更新:

删除所有现有的“蝴蝶”并使用新的非常相似的名称创建新的

string butterfly2;

butterfly2 = "butterfly without error" <-- worked for me perfectly.

*第一次尝试:

  1. 清理和重建项目 - 没有帮助
  2. 重新启动整个程序(IDE) - 没有帮助*
于 2020-10-19T10:57:00.673 回答