0

I am writing the following program in which I have to access variable "lat" outside the if statements. But compiler is showing me an error while using this statement.

int LAC = Convert.ToInt32(lat[0], 16);

The error I am getting is:

The name lat does not exist in current context

What can be the possible reason for this? As I am using a string initialized inside an if loop, outside the if loop. Also if I would have declared it local to some function, this error would have been justified but when used inside a loop and then being used outside the loop its showing error. What can be the reason? The code is as follows:

flag = string.Compare(excel_getValue("A" + i), "DATE");
    if (flag == 1)
    {
        string[] date = excel_getValue("A" + i).Split();
    }
    else if (flag != 1)
    {
        string[] lat = excel_getValue("A" + i).Split();
    }

    if (result == 0)
    {
        MessageBox.Show("Location Tracking Complete");
        // Environment.Exit(0);     // program exit
        Thread.Sleep(5000);
    }

    int LAC = Convert.ToInt32(lat[0], 16);    // Converting to int
3
  • what lat should be used if flag==1? you declare it and set its value only in one condition Commented Jan 4, 2017 at 5:38
  • @S.Serp i am fetching this data from the first column of excel sheet. if i will be getting keyword "DATE" in my data it will get stored in date variable and else the data will be stored in lat variable.flag = string.Compare(excel_getValue("A" + i), "DATE"); that's the statement that decide flag value. Commented Jan 4, 2017 at 5:53
  • on getting stored i want to access it but unable to outside the loop of if Commented Jan 4, 2017 at 5:54

3 Answers 3

1

You need to know something about the scope. The scope of you lat variable ends within the else block. So if you need to use it outside the block you need declare before the if statement.

string[] lat = null;
if (flag == 1)
{
    string[] date = excel_getValue("A" + i).Split();

}
else if (flag != 1)
{
    lat = excel_getValue("A" + i).Split();
}
if (result == 0)
{
    MessageBox.Show("Location Tracking Complete");
    //Environment.Exit(0); // programme exit
    Thread.Sleep(5000);
}
//This prevents from throwing unwanted exception.
if(lat != null)
    int LAC = Convert.ToInt32(lat[0], 16);  //Converting to int
Sign up to request clarification or add additional context in comments.

3 Comments

lat isn't always set to a value before being used is the main issue, but there's a lot else.
@Prabu its working now ,so it can be concluded that on initializing plus declaring variable as null outside the if loop m able to access it anywhere in the function.
@pranjalkhanduri you need to check for null before accessing such type of variable as it might through nullreference exception.
0

Declaring a Variable inside the if condition context is not a proper way.The scope of the variable will be only inside the condition therefore using it outside will yield you an error like "the name lat does not exist in current context".

         Void Somefunction()
             {

             string[] lat=null;


          if(true)
          {
               lat=new string[10];
             lat[0]="welcome";
              }
           Console.Writeline(lat[0]);

             }

Event though the above code is correct there will occur an error if condition is false and lat would be null.

Comments

0

I think you are missing most of the comments here. The lat variable as shown in your code, only exist inside THAT if statement... as soon as you leave the if statement lat no longer exist. As below:

else if (flag != 1)
{
    string[] lat = excel_getValue("A" + i).Split();
}
 // lat DNE here

Even if you create a string[] lat; variable before the if statement, the compiler will complain because when you are initializing the lat variable later in an if statement, the compiler will complain because there is a possibility that lat will not get set to some value… i.e when the if statement fails.

Since you do not know the size you need before you get the value from a split, it may be easier to simply set the lat and date variables and avoid the if statements altogether. It is not clear what the other variables do later in your code, but using if statements to create variables is going to create possible initialization/existence errors. Something like below may be a better approach.

flag = string.Compare(excel_getValue("A" + i), "DATE");
string[]  lat = excel_getValue("A" + i).Split();
string[] date = excel_getValue("A" + i).Split();

if (result == 0)
{
    MessageBox.Show("Location Tracking Complete");
    // Environment.Exit(0);     // program exit
    Thread.Sleep(5000);
}
If (lat.count > 0)
  int LAC = Convert.ToInt32(lat[0], 16); // creating a variable MAYBE? Possible issues later
else
  // lat is empty

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.