Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Easy Type Conversion

I'm looking for an easier way to do this. Using the System.Convert.ChangeType function requires a Type object as one of the parameters. I am unable to find a good source for that object. Currently I have to dimension an instance of the object and then call the .GetType method... There has got to be a better way!

Brendan Erwin
Tuesday, March 04, 2003

How about using the static Type.GetType("TypeName") method?

Mike Gunderloy
Tuesday, March 04, 2003

For basic conversions you can use Convert.ToXXX() methods.  Hope I understood properly.

Dan
Tuesday, March 04, 2003

Probably, you should use "is" and "as" C# operators?

Mikhail Andronov
Wednesday, March 05, 2003

Mike thanks. "Type.GetType("TypeName") " was what I was looking for. I never thought of that one.... Does anyone else think its strange that the GetType method of the Type object (static it may be) doesnt return the Type of the Type object like GetType does everywhere else? :) funky, although I guess it makes sense.....
Thanks again man.

Brendan Erwin
Wednesday, March 05, 2003

You know, I thought about it. That DOESNT make sense at all. Methods of the Object class shouldnt be overloaded to add additional functionality. It just confuses the issue. I, and I think many others, look for functionality by browsing the .[LIST 'O' COMMANDS] popup and the object browser. If that command had been listed as something else I think I would have seen it. As it is, my mind is already trained to overlook stuff that looks like basic object stuff...
ok, thats my rant.

Brendan Erwin
Wednesday, March 05, 2003

If you know the type at compile time, then using the C# typeof operator is even better (better performance & some compiler validation).  Confusingly enough the VB equivalent is the GetType operator, not the TypeOf operator which does something completely different.

C#:
System.Type t = typeof(int);

VB:
Dim t As System.Type = GetType(Integer)

Russ
Wednesday, March 05, 2003

[You know, I thought about it. That DOESNT make sense at all. Methods of the Object class shouldnt be overloaded to add additional functionality]

But sometimes it makes sense. For instance I have a MailMessage class that overrides the .ToString() method and instead of returning the Class Type name like most classes do it returns the message as a formatted string, which people requested. I guess I could have used another method name, like .ToRFC822Formatted String() but it seemed like an obvious leap to overload the .ToString().

Ian Stallings
Thursday, March 06, 2003

Where does it says that ToString() returns the class name?

It even says on the docs that ToString():
"Returns a String that represents the current Object".

If you do a ToString on an int, you won't get "System.Int32" but the numeric representation of that int.

Joao Paulo Carreiro
Friday, March 07, 2003

I guess the point of my gripe is that the .GetType method has an established functionality. The static version off the Type class changes that behavior. In contrast, the ToString method DOESNT behave so consistantly so it is less of a shock when its behavior changes.

Brendan Erwin
Friday, March 07, 2003

*  Recent Topics

*  Fog Creek Home