473,805 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us

Nov 18 '05 #1
7 1705
use a switch() statement and ctype() accordingly

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Alphonse Giambrone" <NO**********@e xample.invalid> wrote in message
news:u%******** ********@TK2MSF TNGP11.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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us

Nov 18 '05 #2
Alphonse,

You could do this:

Dim i As Integer = CType("1000", System.Type.Get Type("System.In t32"))

Where:
System.Type.Get Type([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" <NO**********@e xample.invalid> wrote in message
news:u%******** ********@TK2MSF TNGP11.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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us

Nov 18 '05 #3
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" <NO**********@e xample.invalid> wrote in message
news:u#******** ******@TK2MSFTN GP11.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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us

Nov 18 '05 #4
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 customdatasolut ions dot us
"Curt_C [MVP]" <software_AT_da rkfalz.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
use a switch() statement and ctype() accordingly

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Alphonse Giambrone" <NO**********@e xample.invalid> wrote in message
news:u%******** ********@TK2MSF TNGP11.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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us


Nov 18 '05 #5
Thanks Justin,

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

--

Alphonse Giambrone
Email: a-giam at customdatasolut ions dot us
"S. Justin Gengo" <sj*****@aboutf ortunate.com> wrote in message
news:ud******** ******@tk2msftn gp13.phx.gbl...
Alphonse,

You could do this:

Dim i As Integer = CType("1000", System.Type.Get Type("System.In t32"))

Where:
System.Type.Get Type([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" <NO**********@e xample.invalid> wrote in message
news:u%******** ********@TK2MSF TNGP11.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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us


Nov 18 '05 #6
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
"LastDatabaseCo mpact").
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(ValueFiel d, TypeField) '(this obviously would not work as
is).

--

Alphonse Giambrone
Email: a-giam at customdatasolut ions dot us
"Kevin Spencer" <ke***@takempis .com> wrote in message
news:eS******** ******@TK2MSFTN GP09.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" <NO**********@e xample.invalid> wrote in message
news:u#******** ******@TK2MSFTN GP11.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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us


Nov 18 '05 #7
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.Get Type(...). 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 computers security.

This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
Reply-To: "Alphonse Giambrone" <NO**********@e xample.invalid>
From: "Alphonse Giambrone" <NO**********@e xample.invalid>
References: <u#************ **@TK2MSFTNGP11 .phx.gbl> <ud************ **@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: <eJ************ **@TK2MSFTNGP11 .phx.gbl>
Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
NNTP-Posting-Host: ool-4352027f.dyn.op tonline.net 67.82.2.127
Path: cpmsftngxa07.ph x.gbl!cpmsftngx a06.phx.gbl!cpm sftngxa09.phx.g bl!TK2MSFTNGP08 .
phx.gbl!TK2MSFT NGP11.phx.gbl Xref: cpmsftngxa07.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1998 30
X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet

Thanks Justin,

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

--

Alphonse Giambrone
Email: a-giam at customdatasolut ions dot us
"S. Justin Gengo" <sj*****@aboutf ortunate.com> wrote in message
news:ud******** ******@tk2msftn gp13.phx.gbl...
Alphonse,

You could do this:

Dim i As Integer = CType("1000", System.Type.Get Type("System.In t32"))

Where:
System.Type.Get Type([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" <NO**********@e xample.invalid> wrote in message
news:u%******** ********@TK2MSF TNGP11.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","I nteger")

In other words the type conversion would be done programmaticall y, 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 customdatasolut ions dot us




Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1685
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s) { this.s=s;
7
2243
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the collection. Thus, my interface now looks like this public interface IMyCollection : ICollection { void Add (string toBeAdded);
27
5641
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
1882
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict with me... using System.Collections.Generic;
16
12803
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
1887
by: Martin v. Lwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version: $Revision: 42333 $
1
3286
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming -- Deep Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line 10 is illegal? Is what Keith Thompson said in another post also helpful to understand this question: "... The implicit conversion rule applies only to void*, not...
669
26274
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
4
2113
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) : x0(a), y0(b),
8
3153
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the developer. x = someObject as MySpecificType; x = (MySpecificType) someObject; Thanks.
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.