简体   繁体   中英

Why can't I use a local variable inside of a Using block?

I'm still learning the particulars of C# so please forgive me if this rudimentary. I have been looking for an answer to this but haven't found one.

I have declared the local variable (in my function):

string myVar;

I keep getting the error, "Use of unassigned local variable" when I try to return it:

return (myVar);

What am I doing wrong here?

public string GetSomethingFromDB()
{
    string connectionString;
    string sql;
    string myVar;

    connectionString = "Data Source=MyServer; Port=MyPort; User ID=someuser; Password=mypassword; Database=myDatabase";
    sql = "select something from something";
    using (AseConnection conn = new AseConnection(connectionString))
    {
        using (AseCommand cmd = new AseCommand(sql, conn))
        {
            try
            {
                conn.Open();
                using (AseDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        myVar= reader.GetString("column1").Trim();
                    }
                    reader.Close();
                    reader.Dispose();
                }
                conn.Close();
                conn.Dispose();
                cmd.Dispose();
                return (myVar);
            }
            catch (AseException ex)
            {
                //do some stuff
            }
            finally
            {
                 //do some stuff
            }
        }
    }
} 

This has nothing to do with the using statement, and everything to do with the while loop. Leaving everything else aside, this is the problem, in essence:

string myVar;
while (someNonConstantCondition)
{
    myVar = someValue;
}
return myVar;

The compiler is saying that the while loop body may never execute because the condition may be false.

I would suggest changing the code to:

if (reader.Read())
{
    return reader.GetString("column1").Trim();
}

... and use using statements to make sure that all your commands etc are disposed automatically. (You've already got those using statements, so you can remove the calls to conn.Close() , conn.Dispose() and cmd.Dispose() anyway.)

Note that this will only read the first row from the results, whereas you're currently returning the value from the last row. If that's important to you, there may be bigger design problems.

Next you'll need to consider what happens if reader.Read() returns false ... do you want to throw an exception? Return something else? By putting the return statement directly at the place where you've managed to read some data, the question of what happens if you can't read some data is clearer, IMO.

To fix the error change -

string myVar = string.Empty;

The problem lies here

while (reader.Read())
{
    myVar= reader.GetString("column1").Trim();
}

This section might not run and myVar may never be assigned, hence the error.

The compiler is not "sure" whether myVar will get value in loops. Initialize the variable:

string myVar = "";

in case of no data to read, myVar will have no any value,

1) set a value before while block ,

myVar = string.Empty;

or better 2) assign an initial value where you declare the variable,

string myVar = string.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