1

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?

Ben
  • 2,433
  • 5
  • 39
  • 69
  • 1
    you know what will be there in line. but compiler doesn't. it always care about if and else part both. – Amit Jun 22 '18 at 03:31
  • If you've produced a situation where it's impossible that your program will ever be fed an ill-formed file, congratulations. However, it's supremely unlikely that that is true. So what you "know" to be true isn't actually. – Damien_The_Unbeliever Jun 22 '18 at 05:42
  • @Damien_The_Unbeliever apt name haha. And I suppose, you are correct, since there *is* a user element in this haha – Ben Jun 22 '18 at 05:45
  • what's the purpose of `Readfile` ? it looks it has no output (neither via return value nor via side effects (assigning variable with higher scope)). – Spotted Jun 22 '18 at 06:19
  • @Spotted this is only halfway through development. – Ben Jun 22 '18 at 06:40
  • Another consideration, what do you do with datatable containg data from `Line1, Line2, Line3` when you encounter `Line4` ? It is lost right ? It looks like a bug to me. – Spotted Jun 22 '18 at 06:54
  • @Spotted, again, that is not the issue I am dealing with in this situation. I have taken an excerpt from my code, relevant to the problem. – Ben Jun 22 '18 at 07:29
  • Okay, I was asking these questions because I am willing to help you beyond your initial question. Indeed, the fact that you have been brought to ask this question is a sign that something might be wrong, designly speaking. – Spotted Jun 22 '18 at 08:04

2 Answers2

3

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

DataTable table = null;
Backs
  • 24,430
  • 5
  • 58
  • 85
0

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
    }
}
Amit
  • 1,821
  • 1
  • 17
  • 30