Dynamically creating type of System.Int32, how??

  • Thread starter =?iso-8859-1?Q?S=F8ren_M._Olesen?=
  • Start date
?

=?iso-8859-1?Q?S=F8ren_M._Olesen?=

Hi

How do I dynamically create a type of System.Int32 with the value 123?? I've tried the following:

Dim keyci As ConstructorInfo = Type.GetType("System.Int32").GetConstructor(New Type() {Type.GetType("System.Int32")})
Dim key As Object = keyci.Invoke(New Object() {"123"})


however it doesn't seem to work....

TIA

Søren
 
H

Herfried K. Wagner [MVP]

Søren M. Olesen said:
How do I dynamically create a type of System.Int32 with the value 123??
I've tried the following:

Dim keyci As ConstructorInfo =
Type.GetType("System.Int32").GetConstructor(New Type()
{Type.GetType("System.Int32")})
Dim key As Object = keyci.Invoke(New Object() {"123"})

however it doesn't seem to work....

.... because 'Int32' doesn't have a parameterized constructor.
 
S

Søren M. Olesen

I kind of figured that, but is there a way to do it, or do I have to use a
select case and then cast on the type??

Søren
 
J

Jay B. Harlow [MVP - Outlook]

Søren,
It appears you simply want to convert a String to an arbitrary type.

Have you considered Convert.ChangeType?

Something like:

Dim key As Object = Convert.ChangeType("123",
Type.GetType("System.Int32"))

Where the GetType changes based on outside criteria.

However if you know you will always be using Int32, then a simply cast is
easier:

Either of the following:
key = CInt("123")
key = Convert.ToInt32("123")


Hope this helps
Jay

Hi

How do I dynamically create a type of System.Int32 with the value 123?? I've
tried the following:

Dim keyci As ConstructorInfo =
Type.GetType("System.Int32").GetConstructor(New Type()
{Type.GetType("System.Int32")})
Dim key As Object = keyci.Invoke(New Object() {"123"})


however it doesn't seem to work....

TIA

Søren
 

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