Home > Archive > C# > October 2005 > Casting without switch structure?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Casting without switch structure?
|
|
|
| I return (as a DataTable) the table schema information on various
columns in the database. For any column that has one I am accessing
the DEFAULT value specified in the database for that column. The
default value is returned as a string (e.g. "0" on a boolean or
"getdate()" on a datetime). In addition, because I have a working
dataset including schema, I can easily look up the DBType of the column
whose default I am working with. I would like to dynamically cast the
default value without using a switch statement.
foreach(DataRow row in rows)
{
string tablename = row["TABLE_NAME"].ToString();
string columnname = row["COLUMN_NAME"].ToString();
object defaultvalue = row["COLUMN_DEFAULT"];
DataColumn dc = ds.Tables[tablename].Columns[columnname];
// the following line does not work, but illustrates my intent
dc.DefaultValue = Convert.ChangeType(defaultvalue, dc.DataType);
}
In the past I have accomplished this with a switch statement similar
to:
object CastValue(DataColumn dc, string value)
{
object castvalue = null;
switch(dc.DataType.ToString())
{
case "System.Boolean":
castvalue = bool.Parse(value);
break;
case ...
case ... // and so on...
}
}
Obviously, the switch statement poses more work than seems necessary.
Any shortcuts for accomplishing the same? Even if the specifics of
this example are not practical, it seems useful to be able to
"dynamically cast" object values to native datatypes.
I would truly appreciate any feedback. Thanks.
Mario T. Lanza
Clarity Information Architecture, Inc.
| |
| Mitch Denny 2005-10-27, 7:00 pm |
| I don't think you mean casting here, it isn't possible to cast a data-type
unless a valid cast is already defined. About the best you could do is write
a routine to take the value and find a TypeConverter that knows how to do
the data-type transformation. It just depends on how generic you want to
make the algorithm.
--
Mitch Denny
email: mitch.denny@notgartner.com
mobile: +61 (414) 610-141
blog: http://notgartner.com
skype: callto://mitchdenny
"Mario" <mlanza@comcast.net> wrote in message
news:1129739449.159032.102230@o13g2000cwo.googlegroups.com...
>I return (as a DataTable) the table schema information on various
> columns in the database. For any column that has one I am accessing
> the DEFAULT value specified in the database for that column. The
> default value is returned as a string (e.g. "0" on a boolean or
> "getdate()" on a datetime). In addition, because I have a working
> dataset including schema, I can easily look up the DBType of the column
> whose default I am working with. I would like to dynamically cast the
> default value without using a switch statement.
>
> foreach(DataRow row in rows)
> {
> string tablename = row["TABLE_NAME"].ToString();
> string columnname = row["COLUMN_NAME"].ToString();
> object defaultvalue = row["COLUMN_DEFAULT"];
> DataColumn dc = ds.Tables[tablename].Columns[columnname];
> // the following line does not work, but illustrates my intent
> dc.DefaultValue = Convert.ChangeType(defaultvalue, dc.DataType);
> }
>
> In the past I have accomplished this with a switch statement similar
> to:
>
> object CastValue(DataColumn dc, string value)
> {
> object castvalue = null;
>
> switch(dc.DataType.ToString())
> {
> case "System.Boolean":
> castvalue = bool.Parse(value);
> break;
> case ...
> case ... // and so on...
> }
> }
>
> Obviously, the switch statement poses more work than seems necessary.
> Any shortcuts for accomplishing the same? Even if the specifics of
> this example are not practical, it seems useful to be able to
> "dynamically cast" object values to native datatypes.
>
> I would truly appreciate any feedback. Thanks.
>
> Mario T. Lanza
> Clarity Information Architecture, Inc.
>
|
|
|
|
|