Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Type Conversion (http://www.velocityreviews.com/forums/t69891-type-conversion.html)

Alphonse Giambrone 01-05-2004 05:10 PM

Type Conversion
 
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




Curt_C [MVP] 01-05-2004 05:28 PM

Re: Type Conversion
 
use a switch() statement and ctype() accordingly

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


"Alphonse Giambrone" <NOSPAMa-giam@example.invalid> wrote in message
news:u%23zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> 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
>
>
>




S. Justin Gengo 01-05-2004 05:41 PM

Re: Type Conversion
 
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" <NOSPAMa-giam@example.invalid> wrote in message
news:u%23zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> 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
>
>
>




Kevin Spencer 01-05-2004 05:55 PM

Re: Type Conversion
 
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" <NOSPAMa-giam@example.invalid> wrote in message
news:u#zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> 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
>
>
>




Alphonse Giambrone 01-05-2004 08:57 PM

Re: Type Conversion
 
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:%23XIQ1D70DHA.1676@TK2MSFTNGP12.phx.gbl...
> use a switch() statement and ctype() accordingly
>
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> www.Darkfalz.com
>
>
> "Alphonse Giambrone" <NOSPAMa-giam@example.invalid> wrote in message
> news:u%23zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> >
> >

>
>




Alphonse Giambrone 01-05-2004 09:00 PM

Re: Type Conversion
 
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" <sjgengo@aboutfortunate.com> wrote in message
news:udPfbM70DHA.2408@tk2msftngp13.phx.gbl...
> 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" <NOSPAMa-giam@example.invalid> wrote in message
> news:u%23zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> >
> >

>
>




Alphonse Giambrone 01-05-2004 09:11 PM

Re: Type Conversion
 
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" <kevin@takempis.com> wrote in message
news:eSdaEX70DHA.2568@TK2MSFTNGP09.phx.gbl...
> 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" <NOSPAMa-giam@example.invalid> wrote in message
> news:u#zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> >
> >

>
>




Mike Moore [MSFT] 01-06-2004 12:35 AM

Re: Type Conversion
 
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" <NOSPAMa-giam@example.invalid>
> From: "Alphonse Giambrone" <NOSPAMa-giam@example.invalid>
> References: <u#zpb760DHA.3656@TK2MSFTNGP11.phx.gbl>

<udPfbM70DHA.2408@tk2msftngp13.phx.gbl>
> 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: <eJElp780DHA.3216@TK2MSFTNGP11.phx.gbl>
> 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" <sjgengo@aboutfortunate.com> wrote in message
> news:udPfbM70DHA.2408@tk2msftngp13.phx.gbl...
> > 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" <NOSPAMa-giam@example.invalid> wrote in message
> > news:u%23zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> > > 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
> > >
> > >
> > >

> >
> >

>
>
>



Alphonse Giambrone 01-06-2004 08:21 PM

Re: Type Conversion
 
Thanks, I guess I will need resort to the switch/case solution.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us


""Mike Moore [MSFT]"" <michmo@online.microsoft.com> wrote in message
news:WqYGxz%230DHA.3088@cpmsftngxa07.phx.gbl...
> 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" <NOSPAMa-giam@example.invalid>
> > From: "Alphonse Giambrone" <NOSPAMa-giam@example.invalid>
> > References: <u#zpb760DHA.3656@TK2MSFTNGP11.phx.gbl>

> <udPfbM70DHA.2408@tk2msftngp13.phx.gbl>
> > 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: <eJElp780DHA.3216@TK2MSFTNGP11.phx.gbl>
> > 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" <sjgengo@aboutfortunate.com> wrote in message
> > news:udPfbM70DHA.2408@tk2msftngp13.phx.gbl...
> > > 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" <NOSPAMa-giam@example.invalid> wrote in message
> > > news:u%23zpb760DHA.3656@TK2MSFTNGP11.phx.gbl...
> > > > 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
> > > >
> > > >
> > > >
> > >
> > >

> >
> >
> >

>




Domilo 10-25-2012 03:07 PM

It's a quite late, but perhapse a solution...
 
Hello,

Quote:

Originally Posted by Alphonse Giambrone (Post 350265)
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


All times are GMT. The time now is 05:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.