Friends, I'm building a simple foreach loop that gets information from a DataTable (tblCORR) and creates 3 other DTs with it (tblCEMVALVE, tblLOG, tblNUMBER). The caveat is that it must use different column types according to what is provided in tblCORR, but of course typeof won't allow row.ItemArray[2] as the type.
tblCORR looks like this:
LAS.V342.B.REM.ST V342_REM bool tblLOG tblVALVE
CEM.CV44.B.REM.ST CV44_REM bool tblCEMVALVE tblVALVE
CEM.FRT01.PTO06.ST FR_PTO06 bool tblLOG tblSTATUS
CEM.GB02.GEAR.CTR RR_GEAR_SEL byte tblLOG tblSTATUS
CEM.PT21.I_PRES.PSI PT21_PRESS Short tblNUMBER tblDATA
RCP.ADD1.NAME.TXT LAS_ADD1 string tblNUMBER tblLAS
Is there a way to do it without having to put a bunch of IFs in each case?
Thanks in advance!
foreach (DataRow row in tblCORR.Rows)
{
switch (row.ItemArray[3].ToString())
{
case "tblCEMVALVE":
{
tblCEMVALVE.Columns.Add(row.ItemArray[1].ToString(), typeof(row.ItemArray[2]));
break;
}
case "tblLOG":
{
tblLOG.Columns.Add(row.ItemArray[1].ToString(), typeof(row.ItemArray[2]));
break;
}
case "tblNUMBER":
{
tblNUMBER.Columns.Add(row.ItemArray[1].ToString(), typeof(row.ItemArray[2]));
break;
}
}
}
With that type names (they're not name of classes/structs in System
namespace) one possible solution is to use a Dictionary
. For example (verbose code for illustration purposes):
Dictionary<object, Type> knownTypes = new Dictionary<object, Type>();
knownTypes.Add("bool", typeof(bool));
knownTypes.Add("byte", typeof(byte));
knownTypes.Add("Short", typeof(short));
knownTypes.Add("string", typeof(string));
To be used as:
tblLOG.Columns.Add(row.ItemArray[1].ToString(), knownTypes[row.ItemArray[2]]);
Some changes you may/should consider:
static
and private
class member.row.ItemArray[2].ToString()
).StringComparer.InvariantCultureIgnoreCase
for dictionary key (if string
) so short
and Short
will be equivalent.tblLOG
, tblNUMBER
and tblCEMVALVE
) or directly search them using their TableName
inside DataSet
.