By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Difficult one.. please help

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
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

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
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

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:eU**************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
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

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #7

P: n/a
On 2004-08-27, <.> <> wrote:
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...

Nov 21 '05 #8

P: n/a
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

Nov 21 '05 #9

P: n/a
..
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 news:un**************@TK2MSFTNGP11.phx.gbl...
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
Nov 21 '05 #10

P: n/a
..
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

<.> wrote in message news:Od*************@TK2MSFTNGP11.phx.gbl...
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


Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.