Ben
Ben

Reputation: 2523

Use of unassigned local variable - but I know that by the time the program reaches it, it will be assigned

So, I have this section of code:

void Readfile()
{
    using (reader = new StreamReader(file))
    {
        string line = "";
        DataTable table;

        // Search for relevant "tables" in the file
        while ((line = reader.ReadLine()) != null)
        {
            if (line.StartsWith("!"))
            {
                table = CreateDataTable(reader, line);
            }
            else
            {
                AddToTable(table); // Error: "Unassigned local variable"
            }
        }
    }
}

DataTable CreateDataTable(StreamReader reader, string line)
{
    if (line.Contains("!CUST"))
    {
        DataTable custTable = new DataTable();
        custTable.TableName = "Customer";

        string[] columns = line.Split(Convert.ToChar(9));

        foreach (string s in columns)
        {
            custTable.Columns.Add(s);
        }
        return custTable;
    }
    return null;
}

The file this program is reading from will always be in this format:

!Line1
Line2
Line3
!Line4
[etc...]

So I know that this code is sound, in terms of "flow". It will always Create the Table first, before it adds to it. However, the way I have structured the code clearly doesn't work.

My original idea was that if I did create the DataTable before hand, (I.e. DataTable table = new DataTable();) then there would be an empty table floating around.

How should this be written?

Upvotes: 1

Views: 392

Answers (2)

Amit
Amit

Reputation: 1857

you are getting lines from a file. which can be any file. ( if it is going in production and user changes that file - even you as programmer will not be sure that first line will starts with !)

Initially you have kept table unassigned and here on this line,

while ((line = reader.ReadLine()) != null)
{
    if (line.StartsWith("!"))
    {
        table = CreateDataTable(reader, line);
    }
    else
    {
        AddToTable(table); // Error: "Unassigned local variable"
    }
}

you are either creating a table or calling AddToTable method passing table into that.

you know that file is having such data that first line of file will always starts with "!" , but compiler cannot be sure with that fact at compile time.

So as there are two cases in while loop : if and else. there are equal chances that flow will either go in if or in else.

So compiler will always get worried that at first iteration if flow goes in else part, by that time table will not be assigned to any value (not even null). So it generated compile time error.

to avoid such error as Backs suggested, initailize table will null (which will be the best solution)

DataTable table = null;

and when you are doing so, for the sake of being in safe side, you should check for table is not null in AddToTable method at first line.

void AddToTable(DataTable table)
{
    if(table != null)
    {
         //your logic
    }
}

Upvotes: 0

Backs
Backs

Reputation: 24903

You know, but not a compiler, so initialize it with null:

DataTable table = null;

Upvotes: 3

Related Questions