Use of unassigned local variable - if statements

PipBoy picture PipBoy · May 4, 2012 · Viewed 12.5k times · Source

I'm doing the following block of code and the compiler is complaining about unassigned local variables and could use some help identifying what's up.

while (rsData.Read())
{
    if (rsData["TYPE"] != DBNull.Value)
        strType = rsData["TYPE"].ToString().Trim();


    if (strType == "01")
    {
        if (rsData["Text"] != DBNull.Value)
            strwho = rsData["Text"].ToString();

        if ((strwho.Length < 10 || (strwho.IndexOf("NULL") > 1)))
            strwho = "";
    }
    else if (strType == "07")
    {
        if (rsData["Text"] != DBNull.Value)
            strmetades = rsData["Text"].ToString();

        if ((strmetades.Length < 10 || (strmetades.IndexOf("NULL") > 1)))
            strmetades = "";
    }

It complains on all of the 'if (strType == "01")' lines and I'm not sure what's up. I've thought of using a switch for this but that seems to get the same issue also.

Any ideas?

Answer

jorgehmv picture jorgehmv · May 4, 2012

when declaring string strType you must assign a value, something like

string strType = null;

More details: Compiler Error CS0165