473,385 Members | 2,004 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,385 software developers and data experts.

Read variable from file and re-create with correct type

Hi, if in a file i have the following:

<variable type="String">Hello World</variable>
<variable type="Int32">29</variable>

How can i create these variables in my application? (Ignore the reading
of the text in, that part is easy).

I guess it would be easy enough by doing a giant "if" statement, but
thats a horrible solution.

if (type == "Int32")
var = Int32.Parse(value);
else if (type == "String")
var = value;

etc etc

Any help or tips?
Jan 31 '06 #1
3 1497

"Mark Ingram" <no****@nowhere.com> wrote in message
news:Oi**************@TK2MSFTNGP10.phx.gbl...
Hi, if in a file i have the following:

<variable type="String">Hello World</variable>
<variable type="Int32">29</variable>

How can i create these variables in my application? (Ignore the reading of
the text in, that part is easy).

I guess it would be easy enough by doing a giant "if" statement, but thats
a horrible solution.

if (type == "Int32")
var = Int32.Parse(value);
else if (type == "String")
var = value;

etc etc
Any help or tips?


1) A dictionary mapping type names to parser delegates is the cleanest
method. It can even be set up or extended from a config file with te aid of
reflection for maximum flexibility.
2) A method that will work for a limited subset of types is simply to use
reflection to look for a Parse method on the type.
Jan 31 '06 #2
I would do this..

<variable type="System.Int32">29</variable>

Notice i fully qualify the type in the Xml.

In code you could do this..

object x = Activator.CreateInstance( type );
x = value;

return x;

HTH

Jan 31 '06 #3
Andy wrote:
I would do this..

<variable type="System.Int32">29</variable>

Notice i fully qualify the type in the Xml.

In code you could do this..

object x = Activator.CreateInstance( type );
x = value;

return x;

HTH


Thats great, just what i needed. Thanks!
Feb 1 '06 #4

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

Similar topics

6
by: Daniel Loose | last post by:
Hello, I have this strange including problem: I want to read a piece of HTML, residing in some file, into a variable, not echo it out. So far so easy - but now the HTML contains a line of...
3
by: ad | last post by:
I have a text file, it is some scripts of SQL. I want to read it into a variable, and set the string variable tto a command string of SqlCommand. How can I read a text file into a string...
1
by: Darrel | last post by:
Is it possible to set a variable in one UserControl that another UserControl can read? For example: page.aspx navigationControl.ascx pageBanner.ascx the navigation control builds a menu...
3
by: jy836 | last post by:
Hi again. <g> This time, I'm having trouble reading a single line from a file. The line reads, "DBPATH=." But when I use the Input function, it only writes to the variable up to the first space in...
11
by: .Net Sports | last post by:
In VB.net, I'm trying to do a couple of things in a couple of different blocks of code. I need to take the first 25 characters of a text file, then append at the end some ellipses and a MORE link...
0
by: TRB_NV | last post by:
I'm on a new hosted server platform which doesn't support PERL, so I'm converting some of my old code so it'll run as ASP. I have a routine I wrote in 1998 where I have a standard template Word...
1
by: walterbyrd | last post by:
I know I can use the file() function to read a file into an array by providing a literal file name between single quotes: $lines = file('literal_file_name'); But, what if I want file() to use...
5
by: john009 | last post by:
hmmmm...thanks to all that are willing to help i am using VB 6...theres more than one question i wish to ask please bare with me...i have to read in information from a file which the program...
6
by: Per Juul Larsen | last post by:
Hi. My Textfile looks like this Variable 1 Variable 2 Variable 3 Variable 4 On opening the application it will read the textfile, and assign each line of variable to at textfield in VB and...
2
by: kimmelsd33 | last post by:
I need to read a tab delimited text file in vb6, and store the first column, first line of data in a variable. I need to do the same with the last line, first column of data. This should store a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...

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.