简体   繁体   中英

C# coding issue

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

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

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.

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… ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM