C# Data Type Comparison

Started by
6 comments, last by TheTroll 17 years, 1 month ago
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;
         }

}

I will forever be a student.
Advertisement
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.
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.
I will forever be a student.
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.
Quote:Original post by Telastyn
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.





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.
I will forever be a student.
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.
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]
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

This topic is closed to new replies.

Advertisement