PC Review


Reply
Thread Tools Rate Thread

Difficult one.. please help

 
 
Guest
Posts: n/a
 
      27th Aug 2004
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

 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      27th Aug 2004
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


 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      27th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      27th Aug 2004
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


 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      27th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      27th Aug 2004
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


 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      27th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
David
Guest
Posts: n/a
 
      27th Aug 2004
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...



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      27th Aug 2004
..
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:unikIt$(E-Mail Removed)...
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


 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      27th Aug 2004
..
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:(E-Mail Removed)...
> 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
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
This should not be so difficult right?? Earl.AKA J.Alladien in access forum!! Microsoft Access 2 17th Aug 2009 02:17 PM
Why is this so difficult??? HoosierAdvisor Microsoft Outlook Contacts 4 11th May 2009 04:48 PM
oh difficult... x tomi3440@yahoo.com Microsoft Excel Programming 3 2nd Apr 2008 02:49 PM
I think this is difficult - can it be done? michael.beckinsale Microsoft Excel Programming 3 27th Jul 2007 10:38 AM
Help, I'm new at this, and I'm sure it's not difficult jennifer Microsoft Access Reports 9 13th Jul 2004 08:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:10 PM.