Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Type Conversion

Reply
Thread Tools

Type Conversion

 
 
Alphonse Giambrone
Guest
Posts: n/a
 
      01-05-2004
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 Giambrone
Email: a-giam at customdatasolutions dot us



 
Reply With Quote
 
 
 
 
Curt_C [MVP]
Guest
Posts: n/a
 
      01-05-2004
use a switch() statement and ctype() accordingly

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com


"Alphonse Giambrone" <(E-Mail Removed)> wrote in message
news:u%(E-Mail Removed)...
> 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 Giambrone
> Email: a-giam at customdatasolutions dot us
>
>
>



 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      01-05-2004
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" <(E-Mail Removed)> wrote in message
news:u%(E-Mail Removed)...
> 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 Giambrone
> Email: a-giam at customdatasolutions dot us
>
>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      01-05-2004
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.

"Alphonse Giambrone" <(E-Mail Removed)> wrote in message
news:u#(E-Mail Removed)...
> 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 Giambrone
> Email: a-giam at customdatasolutions dot us
>
>
>



 
Reply With Quote
 
Alphonse Giambrone
Guest
Posts: n/a
 
      01-05-2004
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


"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:%(E-Mail Removed)...
> use a switch() statement and ctype() accordingly
>
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> www.Darkfalz.com
>
>
> "Alphonse Giambrone" <(E-Mail Removed)> wrote in message
> news:u%(E-Mail Removed)...
> > 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 Giambrone
> > Email: a-giam at customdatasolutions dot us
> >
> >
> >

>
>



 
Reply With Quote
 
Alphonse Giambrone
Guest
Posts: n/a
 
      01-05-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:u%(E-Mail Removed)...
> > 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 Giambrone
> > Email: a-giam at customdatasolutions dot us
> >
> >
> >

>
>



 
Reply With Quote
 
Alphonse Giambrone
Guest
Posts: n/a
 
      01-05-2004
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).

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


"Kevin Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
> "Alphonse Giambrone" <(E-Mail Removed)> wrote in message
> news:u#(E-Mail Removed)...
> > 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 Giambrone
> > Email: a-giam at customdatasolutions dot us
> >
> >
> >

>
>



 
Reply With Quote
 
Mike Moore [MSFT]
Guest
Posts: n/a
 
      01-06-2004
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" <(E-Mail Removed)>
> From: "Alphonse Giambrone" <(E-Mail Removed)>
> References: <u#(E-Mail Removed)>

<(E-Mail Removed)>
> 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: <(E-Mail Removed)>
> 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!cpmsftng xa09.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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:u%(E-Mail Removed)...
> > > 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 Giambrone
> > > Email: a-giam at customdatasolutions dot us
> > >
> > >
> > >

> >
> >

>
>
>


 
Reply With Quote
 
Alphonse Giambrone
Guest
Posts: n/a
 
      01-06-2004
Thanks, I guess I will need resort to the switch/case solution.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


""Mike Moore [MSFT]"" <(E-Mail Removed)> wrote in message
news:WqYGxz%(E-Mail Removed)...
> 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" <(E-Mail Removed)>
> > From: "Alphonse Giambrone" <(E-Mail Removed)>
> > References: <u#(E-Mail Removed)>

> <(E-Mail Removed)>
> > 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: <(E-Mail Removed)>
> > 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!cpmsftng xa09.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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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" <(E-Mail Removed)> wrote in message
> > > news:u%(E-Mail Removed)...
> > > > 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 Giambrone
> > > > Email: a-giam at customdatasolutions dot us
> > > >
> > > >
> > > >
> > >
> > >

> >
> >
> >

>



 
Reply With Quote
 
Domilo Domilo is offline
Junior Member
Join Date: Oct 2012
Posts: 1
 
      10-25-2012
Hello,

Quote:
Originally Posted by Alphonse Giambrone View Post
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Type conversion function for user defined type... zaeminkr@gmail.com C++ 1 05-16-2007 09:00 AM
conversion from const type* to type* allowed? Michal Nazarewicz C Programming 13 01-05-2007 01:54 PM
Conversion from type 'DBNull' to type 'String' is not valid Chris ASP .Net 2 05-11-2006 08:20 AM
conversion from type 'DBNull' to type 'float' is not valid ibiza ASP .Net 2 01-27-2006 09:57 PM
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. luna ASP .Net 1 02-13-2004 01:15 PM



Advertisments