"T!LT3D" wrote...
[quoted text, click to view] > I'm trying to convert data automatically from
> a SqlDataReader into the correct type for its
> class property, it goes something like this but is
> reported syntactilly incorrect....
>
> MyField = (_sr.GetFieldType(i))_sr.GetValue(i)
It's not only syntactically incorrect, it's logically incorrect as well.
In the sentence above it doesn't really seem that you're trying to "convert"
the value, but rather that you're trying to "cast" it, which is a completely
other cup of tea.
When you cast in that manner, you have to use the "identifier" of the class
you're casting to, not an "instance" of the Type-class.
[quoted text, click to view] > ...it complains at the second _sr (; expected),
> is there a nice funky way of doing this without
> creating some kind of conversion matrix i.e...
The second thing about your question is whether you really need to cast or
convert it. What is the type of MyField? If it's of the type "object", the
cast shouldn't really be needed, unless you're missing to give us some other
information.
An example of what you can do is to just get the value:
object MyField = _sr.GetValue(i);
[quoted text, click to view] > if _sr.GetFieldType(i) =
> "aType" MyField =
> (aType)_sr.GetValue(i)
> else if _sr.GetFieldType(i) =
> "bType" MyField =
> (bType)_sr.GetValue(i)
> else if ......
>
> Any help appreciated.
It is *not* possible to change the Type of a variable in the way you're
indicating in the code above.
It seems like you're coming in from the VB-side, as what you have written
indicates some "Variant"-type, the use of "=" in comparisons, as well as the
exclusion of ";" at the end of statements.
My suggestion is that you look into more of object-orientation, the
typesystem and polymorphism, and how that can be used with the class
"System.Object" as the root-class.
Possibly you also could make your question more clear as to *why* you want
to "convert" or cast the value, i.e. how you're going to use the variable
MyField in the further context.
// Bjorn A