|
-
Dec 7th, 2002, 04:00 PM
#1
Thread Starter
Lively Member
Managing object subtypes
Hi all,
I whonder how can I manage an object subtype.
Explain: take a (pseudocode) sub like the following
void DoSomething(object o)
{
switch (o.type)
{
case string
...
case int32
....
}
}
How can I translate in working c#?
Thanx!
-
Dec 7th, 2002, 06:59 PM
#2
Hyperactive Member
That should almost work:
PHP Code:
switch ( o.GetType().ToString() )
{
case "System.String"
break;
case "System.Int32"
break;
default:
//error
break;
}
Last edited by Scott Penner; Dec 7th, 2002 at 07:13 PM.
-scott
he he he
-
Dec 8th, 2002, 03:43 AM
#3
Thread Starter
Lively Member
Originally posted by Scott Penner
That should almost work:
PHP Code:
switch ( o.GetType().ToString() )
{
case "System.String"
...
}
Yes, I thinked at this, but I don't like string comparison for such a case.
-
Dec 8th, 2002, 06:58 AM
#4
yay gay
sometime ago i though of the same and the only thing that really worked was with strings
\m/  \m/
-
Dec 8th, 2002, 07:02 AM
#5
Thread Starter
Lively Member
Originally posted by PT Exorcist
sometime ago i though of the same and the only thing that really worked was with strings
Now I thiked this:
switch (true)
{
case myObject.GetType.Equals("a".GetType)
... is a string
...
}
But I don't like this too much
-
Dec 9th, 2002, 10:39 AM
#6
New Member
I like your last idea, of not using the "ToString" method. How did direct comparison of the "ToType" methods work out for you?
-
Dec 9th, 2002, 01:47 PM
#7
Hyperactive Member
Yeah, but the last example doesn't work in c#.
The case statement must only have constants.
The best way to do this will be with an if/then/else loop.
Then you can compare types directly. If you are working with a large number of types, you could create an array of the types to compare against, then loop through these to perform the comparison.
For instance, you could have an array consisting of a class that holds two properties, an enumeration value and a class type. Once the comparison returns true, you store the corresponding enumeration value. You can then use this enumeration value in your case statement and go from there.
-scott
he he he
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|