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?