Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Cast to a type obtained with GetType???

Cast to a type obtained with GetType???

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Don Miguel
    wrote on last edited by
    #1

    Hello everyone, The problem is simple: in my class I obtained a type using reflection GetType. Type t = Type.GetType(my_StringT); NOW: how should I cast a variable to type obtained??? simple cast doesn't work... Object my_NewType = (t) my_OldType; -> here the compiler told that type or namespace 't' could not be found. So, how should I use obtained type to cast my variable???? Thanks, Don Miguel.

    L 1 Reply Last reply
    0
    • D Don Miguel

      Hello everyone, The problem is simple: in my class I obtained a type using reflection GetType. Type t = Type.GetType(my_StringT); NOW: how should I cast a variable to type obtained??? simple cast doesn't work... Object my_NewType = (t) my_OldType; -> here the compiler told that type or namespace 't' could not be found. So, how should I use obtained type to cast my variable???? Thanks, Don Miguel.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Don Miguel wrote: So, how should I use obtained type to cast my variable???? You dont. ;P This has riddled my mind a few times, but I have realise this is NEVER necessary. MyDUMeter: a .NET DUMeter clone
      "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

      D 1 Reply Last reply
      0
      • L leppie

        Don Miguel wrote: So, how should I use obtained type to cast my variable???? You dont. ;P This has riddled my mind a few times, but I have realise this is NEVER necessary. MyDUMeter: a .NET DUMeter clone
        "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

        D Offline
        D Offline
        Don Miguel
        wrote on last edited by
        #3

        leppie wrote: You dont. Is this impossible?? leppie wrote: this is NEVER necessary. how is that????:confused::confused: I don't understand.

        L 1 Reply Last reply
        0
        • D Don Miguel

          leppie wrote: You dont. Is this impossible?? leppie wrote: this is NEVER necessary. how is that????:confused::confused: I don't understand.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Don Miguel wrote: how is that???? I don't understand What exactly is the point of this code?

          Type t = Type.GetType(my_StringT);
          Object my_NewType = (t) my_OldType;

          Line 1: You are getting a Type object called my_StringT. Fair. Line 2: Now this line screams of errors! lets start right to left. - what is my_OldType? OK, the compiler cant assume, but I can. Is it a Type object or an instance of that Type? See? - (t) . You can only cast to a type. Is t a type? No, it is a Type object. A class name is a type. - Object my_NewType: Why are you casting in the first place? All objects downcast automatically to their parent types, iow object.Just like most OO languages, everything is an object of type Object (the root/parent/mother class). Now think again what your are trying to do, and if in fact it is necessary. You get what I am saying? ;P Feel free to ask some more questions. Cheers MyDUMeter: a .NET DUMeter clone
          "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

          D 1 Reply Last reply
          0
          • L leppie

            Don Miguel wrote: how is that???? I don't understand What exactly is the point of this code?

            Type t = Type.GetType(my_StringT);
            Object my_NewType = (t) my_OldType;

            Line 1: You are getting a Type object called my_StringT. Fair. Line 2: Now this line screams of errors! lets start right to left. - what is my_OldType? OK, the compiler cant assume, but I can. Is it a Type object or an instance of that Type? See? - (t) . You can only cast to a type. Is t a type? No, it is a Type object. A class name is a type. - Object my_NewType: Why are you casting in the first place? All objects downcast automatically to their parent types, iow object.Just like most OO languages, everything is an object of type Object (the root/parent/mother class). Now think again what your are trying to do, and if in fact it is necessary. You get what I am saying? ;P Feel free to ask some more questions. Cheers MyDUMeter: a .NET DUMeter clone
            "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

            D Offline
            D Offline
            Don Miguel
            wrote on last edited by
            #5

            Thanks for your patience with me, until now. leppie wrote: Is t a type? No, it is a Type object. A class name is a type. With above, you pointed me in right direction. I was really confused, finally I realise what I intent to do. Was a mess in my mind ;) Don Miguel

            L 1 Reply Last reply
            0
            • D Don Miguel

              Thanks for your patience with me, until now. leppie wrote: Is t a type? No, it is a Type object. A class name is a type. With above, you pointed me in right direction. I was really confused, finally I realise what I intent to do. Was a mess in my mind ;) Don Miguel

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Don Miguel wrote: Thanks for your patience with me, until now. Hey, I never stop being patient... Don Miguel wrote: I realise what I intent to do. Was a mess in my mind That was what I was hoping you would see ;P MyDUMeter: a .NET DUMeter clone
              "Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups