cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank

    cast

    Hello,
    I don't know how to do the following.
    In a method I get a parameter with type object. These can be about 10
    different types and I want to store the object in a list with the correct
    type (so, not type object, but type A or B or C). I could use a switch on
    gettype and then do a cast and put it in the list. But if new types are
    added I have to change this method and I am too lazy to do that. I need
    something like:
    list.add((getty pe(object))obje ct)
    The '(gettype(objec t))' is meant as a cast, but this is not possible in c#.
    How can I add the correct type in a generic way? So I don't need to change
    anything if a new type is created.

    Thanks
    Frank


  • Jon Skeet [C# MVP]

    #2
    Re: cast

    On Aug 7, 12:53 pm, "Frank" <frankn...@advi tronic.nlwrote:
    I don't know how to do the following.
    In a method I get a parameter with type object. These can be about 10
    different types and I want to store the object in a list with the correct
    type (so, not type object, but type A or B or C). I could use a switch on
    gettype and then do a cast and put it in the list. But if new types are
    added I have to change this method and I am too lazy to do that. I need
    something like:
    list.add((getty pe(object))obje ct)
    The '(gettype(objec t))' is meant as a cast, but this is not possible in c#.
    How can I add the correct type in a generic way? So I don't need to change
    anything if a new type is created.
    What's the type of your list? If you're adding it to the same list
    whatever happens, then just add it as it is. Casting wouldn't actually
    change the type of the object anyway.

    If you could provide a short but complete program which demonstrates
    the problem, that would make things clearer.

    See http://pobox.com/~skeet/csharp/complete.html for what I mean by
    that.

    Jon

    Comment

    • madhatter2647@gmail.com

      #3
      Re: cast

      This might sound stupid, but why not just create a list of objects, to
      which you can add whatever you'd like, and let the function that gets
      the data from the list perform the cast?

      Comment

      • Frank

        #4
        Re: cast

        you are absolutely right, it's a stupid question. I didn't think of the
        consequences.
        Frank
        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:1186489004 .815582.290570@ d55g2000hsg.goo glegroups.com.. .
        On Aug 7, 12:53 pm, "Frank" <frankn...@advi tronic.nlwrote:
        >I don't know how to do the following.
        >In a method I get a parameter with type object. These can be about 10
        >different types and I want to store the object in a list with the correct
        >type (so, not type object, but type A or B or C). I could use a switch on
        >gettype and then do a cast and put it in the list. But if new types are
        >added I have to change this method and I am too lazy to do that. I need
        >something like:
        >list.add((gett ype(object))obj ect)
        >The '(gettype(objec t))' is meant as a cast, but this is not possible in
        >c#.
        >How can I add the correct type in a generic way? So I don't need to
        >change
        >anything if a new type is created.
        >
        What's the type of your list? If you're adding it to the same list
        whatever happens, then just add it as it is. Casting wouldn't actually
        change the type of the object anyway.
        >
        If you could provide a short but complete program which demonstrates
        the problem, that would make things clearer.
        >
        See http://pobox.com/~skeet/csharp/complete.html for what I mean by
        that.
        >
        Jon
        >

        Comment

        Working...