How to cast to a type stored in a field
-
How do I use a field storing Type information?
private Type _DataType;
private Dictionary<int, object> _BlobData;...
_DataType cow = (_DataType)_BlobData[0];
//the previous line shows compiler error
//._DataType' is a 'field' but is used like a 'type'Is "Type" not the right type?
-
How do I use a field storing Type information?
private Type _DataType;
private Dictionary<int, object> _BlobData;...
_DataType cow = (_DataType)_BlobData[0];
//the previous line shows compiler error
//._DataType' is a 'field' but is used like a 'type'Is "Type" not the right type?
Generics is how you do stuff like that.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
How do I use a field storing Type information?
private Type _DataType;
private Dictionary<int, object> _BlobData;...
_DataType cow = (_DataType)_BlobData[0];
//the previous line shows compiler error
//._DataType' is a 'field' but is used like a 'type'Is "Type" not the right type?
redivider, to get data out of the dictionary you have to give it the key. In a dictionary, the key is the first type whatever that is. So since you are using an int, then the integer value that you entered must exist in your dictionary, else it will be null. For a list, this is completely different from a dictionary, and you can just sellect the first field or [0]. int key = 39292; String testObj = "Hello I'm a string"; object value = (object)testObj; so in your example... _BlobData.add(key, value); To get out the value... you have to put in the correct key... String myOldDefinition = (String)_BlobData[key]; Console.Writeln("My old string = " + myOldDefinition); HTH, Marc
Life is too short to program in Visual Basic.
-
How do I use a field storing Type information?
private Type _DataType;
private Dictionary<int, object> _BlobData;...
_DataType cow = (_DataType)_BlobData[0];
//the previous line shows compiler error
//._DataType' is a 'field' but is used like a 'type'Is "Type" not the right type?
redivider wrote:
Is "Type" not the right type?
It is, but you are casting to _
DataType
, which is a variable, not a data type. Seems that you are confusing the two concepts of data type and variable (i.e. an instance of a data type). Besides that, theObject.GetType()
method is the way to get type information from an object, casting will generally not work. So your code snippet should read something like this:private System.Type _Cow;
private Dictionary<int, object> _BlobData;
...
_Cow = _BlobData[0].GetType();Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software. -
redivider wrote:
Is "Type" not the right type?
It is, but you are casting to _
DataType
, which is a variable, not a data type. Seems that you are confusing the two concepts of data type and variable (i.e. an instance of a data type). Besides that, theObject.GetType()
method is the way to get type information from an object, casting will generally not work. So your code snippet should read something like this:private System.Type _Cow;
private Dictionary<int, object> _BlobData;
...
_Cow = _BlobData[0].GetType();Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.When i have
int i = 10; i = 5 * i;
it knows i'm not trying to multiply 5 * i, but rather 5* the value stored in i. So i'm not trying to cast to a variable (_DataType), but rather the value stored in that variable (int, float, string)... How would generics come into play?