Reputation: 45
I am making a little code, and I am now already running into a error
string getCode(string code)
{
int count = code.Length;
if (count % 2 == 0)
{
string error = "no";
}
else
{
string error = "yes";
string error_message = "There is something wrong with the code. Program.cs - line 23";
}
if (error == "yes")
return null;
return error;
}
Well, now I am setting the variable error
in a if statement. Bu if I later say if (error == "yes")
doesn't work. It gives me the error the name 'error' does not exist in the current content
I am now already search whole the internet for 15-20 minutes. Tryin a lot of fixes and examples. With global variables and even tried to make another class for my variables and read the variables from there. That worked, but then I could not set the variables anymore. Or I just don't know how to do so. Can you please help me? I will be really thankful!
Thanks
Upvotes: 0
Views: 139
Reputation: 53958
You should declare the error
variable out of the scope of if
, like below:
string error = null;
if (count % 2 == 0)
{
error = "no";
}
else
{
error = "yes";
string error_message = "There is something wrong with the code. Program.cs - line 23";
}
if (error == "yes")
return null;
Doing so, you could access this variable either in the block of if
or in the block of else
or more generally speaking in all scopes that would be enclosed in the scope of method getCode
.
side note: error_message
can only be used in the block of else
, and there you just declare a variable and you assign a value to it. You probably have to move also this declaration next to the declaration of error
.
Upvotes: 4
Reputation: 1
string getCode(string code)
{
int count = code.Length;
string error = null;
if (count % 2 == 0)
{
error = "no";
}
else
{
error = "yes";
}
if (error == "yes")
return "something that you intended to return when there is an error";
else
return "something that you intended to return when there is no error";
}
Upvotes: 0
Reputation: 1104
Change your code to:
string getCode(string code)
{
int count = code.Length;
string error = "";
if (count % 2 == 0)
{
error = "no";
}
else
{
error = "yes";
string error_message = "There is something wrong with the code. Program.cs - line 23";
}
if (error == "yes")
return null;
}
What you need to know is that if you declare a variable inside a scope, it is visible inside that scope only. Curly braces define a scope so your error variables were visible inside your if true branch and else branch being two different variables with same name in different scopes.
The changed code moved error variable declaration into containing method scope (getCode) making it accessible.
Upvotes: 0
Reputation: 151
You could make the variable global by creating a class and then calling the class when needing to use this to do this create a class like below:
public static class Globals
{
public static String error = "yes";
}
you can add anymore variables to that when needed to call it use
global.error
Upvotes: 0
Reputation: 814
You have to declare the variables out of the scope to be accessible by the whole code, like
string error="";
string getCode(string code)
{
int count = code.Length;
if (count % 2 == 0)
{
error = "no";
}
else
{
error = "yes";
string error_message = "There is something wrong with the code. Program.cs - line 23";
}
if (error == "yes")
return null;
}
Upvotes: 0