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.
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.
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.
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).
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With