Type Conversion

  • Thread starter Thread starter Alphonse Giambrone
  • Start date 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
 
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
 
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.
 
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
 
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
 
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).
 
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
 
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
 
Back
Top