Kevin
Kevin

Reputation: 1240

Casting Object To Type Of DataColumn

I am making extensive use of dictionaries to decouple some (highly coupled) code. Here are the relevant ones for my question:

//Output table
System.Data.DataTable dtOutput = new System.Data.DataTable();
dtOutput.Columns.Add("Ticket", typeof(string));
dtOutput.Columns.Add("Transit", typeof(string));
dtOutput.Columns.Add("City", typeof(string));
dtOutput.Columns.Add("Province", typeof(string));
dtOutput.Columns.Add("Outage Start Time", typeof(DateTime));
dtOutput.Columns.Add("Outage End Time", typeof(DateTime));
dtOutput.Columns.Add("Priority", typeof(string));
dtOutput.Columns.Add("Business Impact", typeof(TimeSpan));
dtOutput.Columns.Add("Time To Repair (mins)", typeof(Double));
dtOutput.Columns.Add("Summary", typeof(string));

Dictionary<string, int> outputColumnsLegend = new Dictionary<string, int>();
foreach (DataColumn col in dtOutput.Columns)
{
    outputColumnsLegend.Add(col.ColumnName, dtOutput.Columns.IndexOf(col) + 1);
}

Dictionary<string, Variable> outputVariable = new Dictionary<string, Variable>()
{
    {"Ticket", new Variable()},
    {"Transit", new Variable()},
    {"City", new Variable()},
    {"Province", new Variable()},
    {"Outage Start Time", new Variable()},
    {"Outage End Time", new Variable()},
    {"Priority", new Variable()},
    {"Business Impact", new Variable()},
    {"Time To Repair (mins)", new Variable()},
    {"Summary", new Variable()}
};

where Variable is simply:

public class Variable
{
    public object Value { get; set; }
}

Now, to create the output table I am trying to use:

DataRow dataRow = dtOutput.NewRow();
foreach (DataColumn col in dtOutput.Columns)
{
    dataRow[outputColumnsLegend[col.ColumnName]] = (col.DataType)outputVariable[col.ColumnName].Value;
}

However, the reference to col in col.DataType causes this error:

The type or namespace 'col' could not be found (are you missing a using directive or an assembly reference?)

I'm not sure why I am throwing this error or how to fix it. Also, I am not sure if col.DataType should be col.GetType instead (assuming this can be made to work).

Any advice is appreciated.

Regards.

Upvotes: 2

Views: 2963

Answers (1)

dodexahedron
dodexahedron

Reputation: 4657

There's no net result to this code. Because a DataTable stores data as objects (which is why you have to cast when getting data out of them) and since your Variable() class holds an object, no amount of casting is actually going to do any useful work.

Basically, you're unboxing and then immediately re-boxing the data on the same line, even if the cast were made to work.

If you want to preserve types at runtime to avoid this (which is what it seems like you might be trying to do), then you need to make your variable class a generic class like so:

public class Variable<T>
{
   public T Value {get; set;}
}

You would then instantiate specific types of Variable, such as:

Variable<int> someInt;
Variable<string> someString;
etc.

However, even if you do this, you'll still be boxing back as an object the second you place it in a DataTable, so it's an exercise in futility.

Upvotes: 1

Related Questions