473,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to convert a string value to a data type during runtime ?

Hi:
I need to convert a value reading from XML file to specific type,
the XMLstring is look like this:

<Field1 DataType="System.Int32", Value="123">
Dim t As Type = Type.GetType("System.Int32")

Dim i as integer = CType("123", t ) ---> the 2nd param not accept t

Any idea ?

Thansk
JCVoon

Nov 21 '05 #1
4 1426
On 2005-07-20, JC Voon <jc*******@yahoo.com> wrote:
Hi:
I need to convert a value reading from XML file to specific type,
the XMLstring is look like this:

<Field1 DataType="System.Int32", Value="123">
Dim t As Type = Type.GetType("System.Int32")

Dim i as integer = CType("123", t ) ---> the 2nd param not accept t

Any idea ?

Thansk
JCVoon


Dim i As Integer = Convert.ToInt32 ("123")
Dim i As Integer = Integer.Parse ("123")

--
Tom Shelton [MVP]
Nov 21 '05 #2
"JC Voon" <jc*******@yahoo.com> schrieb
Hi:
I need to convert a value reading from XML file to specific type,
the XMLstring is look like this:

<Field1 DataType="System.Int32", Value="123">
Dim t As Type = Type.GetType("System.Int32")

Dim i as integer = CType("123", t ) ---> the 2nd param not accept t

Any idea ?

System.String implements the IConvertible interface. Check out it's ToType
function:

Dim t As Type = Type.GetType("System.Int32")
Dim o As Object = DirectCast("123", IConvertible).ToType(t, Nothing)
o is declared "as Object" because you don't know the type. If you would know
it, you could declare it 'as integer' and use CInt or Integer.Parse.
Armin

Nov 21 '05 #3
"JC Voon" <jc*******@yahoo.com> wrote in message
news:42***************@msnews.microsoft.com...
Dim t As Type = Type.GetType("System.Int32")
Dim i as integer


Essentially, what you /want/ to do is ...

i = Convert.ChangeType( "123", t )

.... but, perversely, ChangeType() returns an Object which you
then have to cast /again/, as in

i = DirectCast( _
Convert.ChangeType( "123", t ) _
)

HTH,
Phill W.
Nov 21 '05 #4
Hi TomShelton, Armin Zinger, Phill. W:

Thanks for the answer, both Armin and Phill. method is exactly what i
want.
Dim t As Type = Type.GetType("System.Int32")
Dim o As Object = DirectCast("123", IConvertible).ToType(t, Nothing)

and

Dim t As Type = Type.GetType("System.Int32")
Dim o As Object = Convert.ChangeType( "123", t )
Thanks
JCVoon

Nov 21 '05 #5

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

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
2
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
2
by: CodeMonkey775 | last post by:
I'm having problems passing a variable to a method which is executed and compiled using CodeDom. The situation is I have a List<CellData> with cells, each containing a formula (like Excel). I am...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.