C# Data Type Comparison

Recommended Posts

jrmiller84    118
Hello everyone, I have been doing some work with data type comparisons and I wanted to get some of your opinions. I have a function that takes in a generic parameter(since we want them to be able to pass any variable type in), determines the real type of the generic, feeds that through a switch statement and does X number of things within that switch statement. I don't know if the way I have gone about solving the problem is the most elegant. It works fine, but I want to make sure there's not a better way. I had a hard time figuring out how to compare data types like this. I've hidden some of the data types in the case statements to make it a little shorter, I am actually referencing all of them in my code. Thanks for your input!
        public void MyTest<T>(T t, ref HtmlTextWriter myOutput)
{

Type PassedType = t.GetType();
TypeCode PassedTypeCode = Type.GetTypeCode(PassedType);

switch (PassedTypeCode)
{
case TypeCode.Boolean:
myOutput.Write("");
break;
case TypeCode.Byte:
myOutput.Write("");
break;
case TypeCode.Char:
myOutput.Write("");
break;
case TypeCode.DateTime:
myOutput.Write("");
break;
}

}



Share this post

Share on other sites
jpetrie    13153
Well, it seems to me that if A<T> for every T has different behavior (implied by your switch), maybe you don't really need a generic here?

Can you expand upon what you're doing, really? Traditionally dynamic dispatch (e.g., virtual functions) or something similar would be used here, instead of typeswitching. Since you seem to care about basic types you obviously cannot add virtual methods to them, but you might be able to leverage some other similar method, such as delegates, depending on what you are really trying to do.

Share this post

Share on other sites
jrmiller84    118
I am attempting to make a method that takes in any type of variable (string, int, bool, etc etc) and based on the type of variable that is passed in, draw some controls on the screen. For example, if they pass in a string, it will draw a text box and the strings value in the text box(this method is within a custom control class) or if they pass in a bool, it will display two radio buttons for on and off.

Share this post

Share on other sites
Telastyn    3777
Care to provide a real example? Are you just writing nothing for types? What are you writing things for? What's the actual purpose?

Based on this, just using overloaded ToString() (perhaps with a decorator) might be sufficient. Otherwise I agree with jpetrie, using some sort of Dictionary<Type,delegate> setup will allow your more flexibility.

Share this post

Share on other sites
jrmiller84    118
Quote:
 Original post by TelastynCare to provide a real example? Are you just writing nothing for types? What are you writing things for? What's the actual purpose?Based on this, just using overloaded ToString() (perhaps with a decorator) might be sufficient. Otherwise I agree with jpetrie, using some sort of Dictionary setup will allow your more flexibility.

I'm not entirely sure what you mean, you want me to sample a call to that function to show how I use it? I'm not writing nothing for the type. Right now I just call it with

MyTest(true, ref output);

And (based on the code I showed you guys) it spits out a blank string to the screen, but I have it coded to show the data type so for me it prints out "System.boolean" instead of blank.

Share this post

Share on other sites
jpetrie    13153
Yeah. Generics are not the proper mechanism for doing what you're trying to do. The entire concept seems a little backwards, really. But I would go with the mapping from types to delegates, or to base class references to a derived control class or something.

Share this post

Share on other sites
gharen2    520
The other issues that have been raised aside, there is a more elegant way to compare data types:

if(t is bool)    DoSomeStuffForBools();if(t is int)    DoSomeStuffForInts();etc..

The "is" keyword is my favorite thing in c#.

But I agree, a hashtable would work best. Get the Type object for each type to use as the key. Have each key match a corresponding class or delegate that does what you want. This way you can also share them between keys. (For example, the keys "System.Single" and "System.Double" can match the same instance of a class that does what you want).

Type key = t.GetType();if(someHashTable.ContainsKey(key))   someHashTable[key].DoSomeThing(t);else   Throw new SomeException();

[Edited by - gharen2 on March 3, 2007 12:26:20 AM]

Share this post

Share on other sites
TheTroll    883
Why don't you just overload the function and that way the right one will get called.

public void MyTest(int t, ref HtmlTextWriter myOutput)
public void MyTest(bool t, ref HtmlTextWriter myOutput)
public void MyTest(string t, ref HtmlTextWriter myOutput)
ect...

Think that would be the better solution. It gets rid of having to have a huge switch statment and dealing with everything in the one function.

theTroll

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now