Results 1 to 7 of 7

Thread: Managing object subtypes

  1. #1

    Thread Starter
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103

    Question 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!
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  2. #2
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    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

  3. #3

    Thread Starter
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103
    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.
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    sometime ago i though of the same and the only thing that really worked was with strings
    \m/\m/

  5. #5

    Thread Starter
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103
    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
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  6. #6
    New Member
    Join Date
    Nov 2002
    Posts
    15
    I like your last idea, of not using the "ToString" method. How did direct comparison of the "ToType" methods work out for you?

  7. #7
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    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
  •  



Click Here to Expand Forum to Full Width