Type Conversion

  • Thread starter Alphonse Giambrone
  • Start date
A

Alphonse Giambrone

How can I convert a string to a different type based on another string or
other variable?

For instance, instead of
Dim i as Integer

i = ctype("1000", Integer)

I would like to do

Dim i as Integer

i = ctype("1000","Integer")

In other words the type conversion would be done programmatically, rather
than specified in the code.

In old VB the 'types' had values (like Integer =3, Long = 4, etc). Is there
something similar in .NET?

TIA
 
S

S. Justin Gengo

Alphonse,

You could do this:

Dim i As Integer = CType("1000", System.Type.GetType("System.Int32"))

Where:
System.Type.GetType([Type Name As String])

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
K

Kevin Spencer

Your question doesn't make sense as stated. What exactly are you trying to
accomplish? What is your requirement?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
A

Alphonse Giambrone

Thanks, that would work, but I thought there should be a more compact way.
It is really not a necessity, just that I feel I am missing something.
--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
 
A

Alphonse Giambrone

Thanks Justin,

That is the type of statement I am looking for, but it does not work.
Error is, System.Type.GetType is undefined.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


S. Justin Gengo said:
Alphonse,

You could do this:

Dim i As Integer = CType("1000", System.Type.GetType("System.Int32"))

Where:
System.Type.GetType([Type Name As String])

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


Alphonse Giambrone said:
How can I convert a string to a different type based on another string or
other variable?

For instance, instead of
Dim i as Integer

i = ctype("1000", Integer)

I would like to do

Dim i as Integer

i = ctype("1000","Integer")

In other words the type conversion would be done programmatically, rather
than specified in the code.

In old VB the 'types' had values (like Integer =3, Long = 4, etc). Is there
something similar in .NET?

TIA
 
A

Alphonse Giambrone

Sorry for the confusion, Kevin.
I know there are other ways to do this, but it is bugging me now.
Simplistically, I want to have a database table with 3 fields.
ItemField (text)
ValueField (text)
TypeField (could be integer, text or whatever is necessary)

ItemField would contain the description of a value to lookup (for example
"LastDatabaseCompact").
ValueField would contain the associated value as a string (for example
"1/4/2004").
TypeField would indicate the type of data in ValueField.

I would then have a function that could look up the value in ValueField for
the requested ItemField and return the result as the appropriate type. In
this example a date.
Skipping all the db connectivity, etc.
It would be something like
Return Ctype(ValueField, TypeField) '(this obviously would not work as
is).
 
M

Mike Moore [MSFT]

Hi Alphonse,

You are getting an object is undefined error because CType does not allow
variables or calculations within the type parameter. The Visual Basic
Language Reference - CType Function documentation shows this:

typename
Any expression that is legal within an As clause in a Dim statement, that
is, the name of any data type, object, structure, class, or interface.

We are not allowed to dim a variable as type System.Type.GetType(...). The
same thing applies to the type parameter of the CType function.

I think you need to use a Select Case statement, as mentioned earlier.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
Reply-To: "Alphonse Giambrone" <[email protected]>
From: "Alphonse Giambrone" <[email protected]>
References: <u#[email protected]>
Subject: Re: Type Conversion
Date: Mon, 5 Jan 2004 16:00:14 -0500
Lines: 72
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: ool-4352027f.dyn.optonline.net 67.82.2.127
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
phx.gbl!TK2MSFTNGP11.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:199830
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Thanks Justin,

That is the type of statement I am looking for, but it does not work.
Error is, System.Type.GetType is undefined.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


S. Justin Gengo said:
Alphonse,

You could do this:

Dim i As Integer = CType("1000", System.Type.GetType("System.Int32"))

Where:
System.Type.GetType([Type Name As String])

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


Alphonse Giambrone said:
How can I convert a string to a different type based on another string or
other variable?

For instance, instead of
Dim i as Integer

i = ctype("1000", Integer)

I would like to do

Dim i as Integer

i = ctype("1000","Integer")

In other words the type conversion would be done programmatically, rather
than specified in the code.

In old VB the 'types' had values (like Integer =3, Long = 4, etc). Is there
something similar in .NET?

TIA
 
A

Alphonse Giambrone

Thanks, I guess I will need resort to the switch/case solution.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


"Mike Moore [MSFT]" said:
Hi Alphonse,

You are getting an object is undefined error because CType does not allow
variables or calculations within the type parameter. The Visual Basic
Language Reference - CType Function documentation shows this:

typename
Any expression that is legal within an As clause in a Dim statement, that
is, the name of any data type, object, structure, class, or interface.

We are not allowed to dim a variable as type System.Type.GetType(...). The
same thing applies to the type parameter of the CType function.

I think you need to use a Select Case statement, as mentioned earlier.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer's security.

This posting is provided "AS IS", with no warranties, and confers no rights.
Subject: Re: Type Conversion
Date: Mon, 5 Jan 2004 16:00:14 -0500
Lines: 72
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: ool-4352027f.dyn.optonline.net 67.82.2.127
Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
phx.gbl!TK2MSFTNGP11.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:199830
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Thanks Justin,

That is the type of statement I am looking for, but it does not work.
Error is, System.Type.GetType is undefined.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


S. Justin Gengo said:
Alphonse,

You could do this:

Dim i As Integer = CType("1000", System.Type.GetType("System.Int32"))

Where:
System.Type.GetType([Type Name As String])

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


How can I convert a string to a different type based on another
string
or
other variable?

For instance, instead of
Dim i as Integer

i = ctype("1000", Integer)

I would like to do

Dim i as Integer

i = ctype("1000","Integer")

In other words the type conversion would be done programmatically, rather
than specified in the code.

In old VB the 'types' had values (like Integer =3, Long = 4, etc). Is
there
something similar in .NET?

TIA
 
Joined
Oct 25, 2012
Messages
1
Reaction score
0
It's a quite late, but perhapse a solution...

Hello,

How can I convert a string to a different type based
i = ctype("1000","Integer")

Instead of "Integer" I found with "Int16" (or Int64...) with:

i = Convert.ChangeType("1000", System.Type.GetType("system.int16", True, True))

Regards.
Domilo
 

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

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top