Cast to a type obtained with GetType???
-
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.
-
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.
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." -
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."leppie wrote: You dont. Is this impossible?? leppie wrote: this is NEVER necessary. how is that????:confused::confused: I don't understand.
-
leppie wrote: You dont. Is this impossible?? leppie wrote: this is NEVER necessary. how is that????:confused::confused: I don't understand.
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." -
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."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
-
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
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."