I am trying to do a switch on a Type. It's actually a DataType used to
type a column in a DataTable.
I have run through several blogs and am not sure what I'm missing or
why it can't be done.

My switch statement looks like this:

int typeEnum = myObject.GetType().GetHashCode();
switch( typeEnum )
{
case ( typeof(System.Int64).GetHashCode() ) :
bool ThisIsABigInt = yes;
break;

That does not work. But this does:

string typeEnum = myObject.GetType().ToString();
switch( typeEnum )
{
case "System.Int64":
bool ThisIsABigInt = yes;
break;


The method that does this switch gets called around a zillion times,
and all of that ToString() seems like a problem.

What is the best way to simply switch on an instance Type to perform
an action? There are 30 Type tests and I don't think writing 30 if
statements is the right way to do things. Is it?

Thanks.

Re: Type switch? by Mattias

Mattias
Mon Nov 28 16:07:07 CST 2005


>What is the best way to simply switch on an instance Type to perform
>an action? There are 30 Type tests and I don't think writing 30 if
>statements is the right way to do things. Is it?

IMO it's better than the two alternatives you provided in your post.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Re: Type switch? by Jay

Jay
Mon Nov 28 20:22:41 CST 2005

xenophon,
> What is the best way to simply switch on an instance Type to perform
> an action? There are 30 Type tests and I don't think writing 30 if
> statements is the right way to do things. Is it?
As Mattias suggests, 30 if statements would be the way I would go.

I would however consider a switch statement on System.TypeCode for the known
TypeCodes, then a series of ifs for the others (System.TimeSpan for example
is not on TypeCode. You can use Type.GetTypeCode to get a type's TypeCode.

Alternatively I would consider creating a "strategy" object where the Type
is used to Index into a dictionary (HashTable) of "strategy" objects. One
specific "strategy" object for each Type you expect.


--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"xenophon" <xenophon@online.nospam> wrote in message
news:75tmo19j4k946rd6veociearjiu5p6o9gi@4ax.com...
>
>
> I am trying to do a switch on a Type. It's actually a DataType used to
> type a column in a DataTable.
> I have run through several blogs and am not sure what I'm missing or
> why it can't be done.
>
> My switch statement looks like this:
>
> int typeEnum = myObject.GetType().GetHashCode();
> switch( typeEnum )
> {
> case ( typeof(System.Int64).GetHashCode() ) :
> bool ThisIsABigInt = yes;
> break;
>
> That does not work. But this does:
>
> string typeEnum = myObject.GetType().ToString();
> switch( typeEnum )
> {
> case "System.Int64":
> bool ThisIsABigInt = yes;
> break;
>
>
> The method that does this switch gets called around a zillion times,
> and all of that ToString() seems like a problem.
>
> What is the best way to simply switch on an instance Type to perform
> an action? There are 30 Type tests and I don't think writing 30 if
> statements is the right way to do things. Is it?
>
> Thanks.
>
>