Saturday, March 31, 2012

string to integer

Hello

I need to do something like this, read data from text file and use it as integer. How can I do it?

Please help me...

Artur

myString = "1234"

myInt = CInt(myString)


Is the text that you are retrieving strictly a number, or is it a combination of text and numbers where you only want to get the numbers? If it's just numbers, you can use Integer.Parse() in VB.NET or int.Parse() in C#.

To read a text file, you use the System.IO namespace. The TextReader Class on MSDN has an example of using a TextReader to get the contents of a text file.

TextReader Class

If the variable m_Text contained the contents of the text file, then here is how you would use Integer.Parse:

VB.NET
Dim m_NumberAs Integer
m_Number =Integer.Parse(m_Text)

C#
int m_Number;
m_Number =int.Parse(m_Text);

HTH


If you are expecting it to be numbers either of the ways above will work however if you try to cast you might want to wrap it in a try to catch any problems in case someone does put in non number data

0 comments:

Post a Comment