Difficult one.. please help

G

Guest

Hi I have trying to solve this but I can't understand why it's not working.

I want to cast a variable dynamically with a type that I pass truogh a string.
I am receiving a value and want to check if the type is correct before saving it to the XML.
I am getting the Type from xsd and pass this to a function to convert the newvalue to the proper type.
I want to avoid doing if statement to check for the type and then try to convert it.

This is an example my code.

Dim strType as string = "System.Int32"
Dim val as object

Dim type As Type = (type.GetType(strType))

val = CType(val, Type)

I have tried also passing a different object of type 'Type'

Is this possible?



Please help
 
C

Cor Ligthert

Helllo mysterious,

Are you inventing the wheel, all is already build in.

Some examples
Dim a As Integer = 1
Dim b As String = "2"
Dim c As Decimal = 10.1D
Dim d As Integer
Dim f As Decimal
Dim g As String
Dim h As Object
d = CInt(b)
g = c.ToString
f = CDec(a)
h = a
d = CInt(h)

You need to cast it to the type you want and especially in that is VBNet
very powerfull. (I only showed you only a very small selection of methods to
do that)

Cor

I want to cast a variable dynamically with a type that I pass truogh a
string.
I am receiving a value and want to check if the type is correct before
saving it to the XML.
I am getting the Type from xsd and pass this to a function to convert the
newvalue to the proper type.
I want to avoid doing if statement to check for the type and then try to
convert it.

This is an example my code.

Dim strType as string = "System.Int32"
Dim val as object
Dim type As Type = (type.GetType(strType))
val = CType(val, Type)
I have tried also passing a different object of type 'Type'
Is this possible?

Please help
 
O

One Handed Man \( OHM - Terry Burns \)

I think your missing his point. He wants to cast at runtime, and use the
typename in a string to determine what type the object will be cast to.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
C

Cor Ligthert

Terry,

I know however I wrote inventing the wheel, why would that be needed, one
reason can be that a lot of people are looking to use the Var from scripting
languages, however when you put a value in a object there is not any need
for that.

You can get it back by just naming the type of the receiving value.

Cor
 
O

One Handed Man \( OHM - Terry Burns \)

In his scenario, he needs to discover the type for his XML storage.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
C

Cor Ligthert

Terry

As sender you can just cast it as I wrote, as a receiving field it is with
the informaiton I have now an almost a ridiculous question.

A kind of inventing the wheel.

I know that you can make a loop in it to fill it, however from where than?

Create an unknown XML file from an unknown table

(when it is a datatable by the way it is)
ds.add.table(x)

Cor
 
O

One Handed Man \( OHM - Terry Burns \)

OK, when you create an XML/XSD combination, you need to know the Type of the
Data. He is saying he does not know this at design time, so he needs to
check this when he creates the XSD. I understand what you are saying, but
I'm not yet convinced that this is enough for the OP, he has not been
verbosely Explicit in what he is trying to achieve, so I think we should
wait until he replies rather than trying to interpolate his post.



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
D

David

This is a multi-part message in MIME format.

------=_NextPart_000_000A_01C48C4E.A386BA70
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

If you could turn that off, it would be nice...
Hi I have trying to solve this but I can't understand why it's not =
working.

The problem is that CType doesn't do what you think it does. It isn't a
runtime function taking a System.Type, it's a keyword that is bound at
compile time.

Also, Type.GetType is really worth avoiding if possible, there's all
kinds of undocumented wackiness in there.

All in all, if it's possible your best bet is to follow Cor's advice.
Have some sort of dispatch function that includes all the types you care
about in a case statement, and hard-code the conversions...

Select Case typeString
Case "System.Int32"
variable = CInt(s)

Case "System.Boolean"
variable = CBool(s)


And so on...
 
J

Jay B. Harlow [MVP - Outlook]

..
It sounds like you want to use Convert.ChangeType.

Dim strType as string = "System.Int32"
Dim val as object

Dim type As Type = (type.GetType(strType))

val = "100"

val = Convert.ChangeType(val, type)

Will convert the string "100" to the integer 100.

See System.Convert class for restrictions.

Hope this helps
Jay


<.> wrote in message Hi I have trying to solve this but I can't understand why it's not working.

I want to cast a variable dynamically with a type that I pass truogh a
string.
I am receiving a value and want to check if the type is correct before
saving it to the XML.
I am getting the Type from xsd and pass this to a function to convert the
newvalue to the proper type.
I want to avoid doing if statement to check for the type and then try to
convert it.

This is an example my code.

Dim strType as string = "System.Int32"
Dim val as object

Dim type As Type = (type.GetType(strType))

val = CType(val, Type)

I have tried also passing a different object of type 'Type'

Is this possible?



Please help
 
J

Jay B. Harlow [MVP - Outlook]

..
In addition to System.Convert class I identified in my earlier post, there
is System.Xml.XmlConvert, however I am not seeing a "ChangeType" on the
XmlConvert class.

The advantage of XmlConvert is it uses XML formatting for the conversion,
the advantage of Convert it is will change the type of an object from one
type to another.

Hope this helps
Jay
 
G

Guest

Hi There,

what I'm need to do is to validate the NewValue before saving it in the xml.
At the moment I am validating the xml/xsd everytime I change a value.

I would like to avoid writting the following
Select Case typeString
Case "System.Int32"
variable = CInt(s)

Case "System.Boolean"
variable = CBool(s)

but for some reason I can't use the following
I am getting the Datatype from the XSD Schema file.
I hope I made myself clearer...

Thanks for the help and coments.

Example of what I am trying to achieve..

covertType ("xx", XMLSchemType)

private function convertType(byref val as string , byref strType as string)

' this is return from XMLSchemaType ---- "System.Int32"
Dim newval as object

Dim myType As Type = (type.GetType(XMLSchemaType))

try
newval = CType(val, myType) ---- although myType is Type it will not be
accepted.. How can I do this..
Catch er as Exeption
console.write ("Value is of wrong data type")
End Try

end function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top