Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use local variable before it is declared

Tags:

I am trying to create a function but I'm getting an error message.

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; 

Cannot use local variable 'startingRadius' before it is declared.

The same problem occurs for the bitmap variable as well. Normally in c++ this type of declaration would work; however, I am unsure why it is not working here.

like image 683
user1084113 Avatar asked Jul 30 '13 00:07

user1084113


People also ask

Can not use local variable before it is declared?

The error message is basically telling you that you have a local variable that you are trying to use which has not been declared. Which suggests that the if (startingRadius == 1) code is actually inside a different method than the method you have declared.

Can you declare local variables?

Declaring Local VariablesYou can declare them at the start of the program, within the main method, inside classes, and inside methods or functions. Depending on where they are defined, other parts of your code may or may not be able to access them. A local variable is one that is declared within a method.

Why the value of local variable is not used?

The "Value not used" is just a warning; it tells you that your variable p only exists within the try-block. You can declare your p-variable before the try - that way, you can use it outside the try-scope(the scope of a variable refers to where it exists, in this case, only inside the try-block).

What is variable declaration?

Declaration of a variable in a computer programming language is a statement used to specify the variable name and its data type. Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it.


2 Answers

In visual studio. Sometimes when you declare a variable again (a second time). It will give this error. For example, this will sometimes throw the exception you mentioned:

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

Obviously this is incorrect anyway. So removing the second declaration (on line 4) will solve the problem.

Note: The exception you would ordinarily expect would be A local variable named 'startingRadius' is already defined in this scope. But for some reason, the exception you mentioned is shown sometimes.

like image 168
gunwin Avatar answered Oct 06 '22 00:10

gunwin


You are missing a closing brace for your method but otherwise this code can compile on my machine... (changed Height to a property as well)

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;          } 
like image 43
Kevin Avatar answered Oct 06 '22 01:10

Kevin