I have an array of objects and I would like to pass it into a method that only accepts DOUBLE
, NULL
, STRING
or DATETIME
. So when I try to pass in the value it gives an error saying I can't pass in any arbitrary object and that it has to be parsed as a DOUBLE
, NULL
, STRING
or DATETIME
first.
foreach (var currRow in dataSet.Tables[0].Rows)
{
var tuple = Com.Tibco.As.Space.Tuple.Create();
//here is where i loop through the object array
for (int i = 0; i < currRow.Values.Length; i++)
{
//here is where i try to pass it to the method (which doesn't accept it)
tuple.Put(dataSet.Tables[0].ColumnNames[i], currRow.Values[i]);
}
inSpace_.Put(tuple);
}
In short I need a way to parse each object and cast it as the appropriate one and then put it inside the tuple.
Edit:
Here's what I tried to do, but it didn't work:
foreach (var currRow in dataSet.Tables[0].Rows)
{
var tuple = Com.Tibco.As.Space.Tuple.Create();
for (int i = 0; i < currRow.Values.Length; i++)
{
if (currRow.Values[i] != null)
{
if (dataSet.Tables[0].ColumnNames[i].GetType().IsEquivalentTo(typeof(DateTime)))
{
DateTime value = DateTime.Parse(dataSet.Tables[0].ColumnNames[i].ToString());
tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
}
else if (dataSet.Tables[0].ColumnNames[i].GetType().IsEquivalentTo(typeof(Double)))
{
Double value = Convert.ToDouble(dataSet.Tables[0].ColumnNames[i]);
tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
}
else
{
string value = dataSet.Tables[0].ColumnNames[i].ToString();
tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
}
}
}
inSpace_.Put(tuple);
}
foreach (var currRow in dataSet.Tables[0].Rows)
{
var tuple = Com.Tibco.As.Space.Tuple.Create();
for (int i = 0; i < currRow.Values.Length; i++)
{
var obj = dataSet.Tables[0].ColumnNames[i], currRow.Values[i];
var value = null;
value = obj as double;
if (!validObject(value)) value as string;
if (!validObject(value)) value as DateTime;
tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
}
inSpace_.Put(tuple);
}
bool validObject(object obj) { return (null != obj); }
Update, based on your edit, I'd use a switch on type instead:
for (int i = 0; i < currRow.Values.Length; i++)
{
var type = obj.GetType();
var obj = dataSet.Tables[0].ColumnNames[i], currRow.Values[i];
var value = null;
switch (type)
{
case DateTime:
value = DateTime.Parse(obj);
break;
case double:
value = Convert.ToDouble(obj);
break;
case string:
value = Convert.ToString(obj);
break;
}
tuple.Put(dataSet.Tables[0].ColumnNames[i], value);
}