C# 在声明局部变量之前不能使用它

C# 在声明局部变量之前不能使用它,c#,function,parameters,scope,C#,Function,Parameters,Scope,我试图创建一个函数,但收到一条错误消息 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 = b

我试图创建一个函数,但收到一条错误消息

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++中,这种类型的声明将起作用;但是,我不确定它为什么不在这里工作。

您的方法缺少一个右大括号,但如果不是这样,此代码可以在我的机器上编译。。。(将高度也更改为属性)


听起来好像您有一个错位的
}
或拼写错误的变量名。如果没有看到完整的代码,我真的说不出来


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

。有时,当您再次声明变量时(第二次)。它将给出这个错误。例如,这有时会引发您提到的异常:

 1.  int startingRadius = 0;
 2.  startingRadius = 5; <-- Exception thrown here.
 3.  
 4.  int startingRadius = 0;
1。int开始半径=0;

2.启动半径=5 对我来说,它有助于删除所有的局部变量,并创建一个不同名称的新变量

Originally:

string butterfly;

butterfly = "butterfly with error"; <-- gives error
最初:
蝴蝶;

butterfly=“出错的蝴蝶”;注意Bitmap.Height是一个属性,所以您只需要
Bitmap.Height
这正是我的问题。谢谢:)Visual Studio应该已更改此误导性错误消息。我喜欢
局部变量。。。已在此范围中定义了错误消息。您好,如果可能,您能否用代码示例进行详细说明?这是有道理的,但对于较新的用户来说,变量范围可能会混淆,而无需详细说明。
Originally:

string butterfly;

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

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