DevPinoy.org
A Filipino Developers Community

>>> First two to make 3 wins! <<<

Convert string to integer

rated by 0 users
This post has 6 Replies | 0 Followers

Top 75 Contributor
Posts 23
Points 440
jpgjuan Posted: 05-04-2008 6:21 PM

This is my code:

Dim strsample, x 

strsample = label.text

 x = strsample + 1

 

Error :Input string was not in a correct format.

 Please help to convert label.text to integer

Thanks in advance 

  • | Post Points: 50
Top 10 Contributor
Posts 749
Points 10,115

 Hmm... have you tried this: http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=163

Top 10 Contributor
Posts 768
Points 13,390

jpgjuan:

This is my code:

Dim strsample, x 

strsample = label.text

 x = strsample + 1

 

Error :Input string was not in a correct format.

 Please help to convert label.text to integer

Thanks in advance 

 

 

Dim strsample, x 

strsample = Val(label.text)

 x = strsample + 1

 

 

 

Convert limitations to great expectations... You are the creative force of your life...

  • | Post Points: 5
Top 10 Contributor
Posts 1,967
Points 39,295

You need to convert the value of label.Text to an numeric value first before you can use it in any mathematical expression.

Use Convert.Int32 method to accomplish what you wanted

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx

devpinoy sig

  • | Post Points: 20
Top 50 Contributor
Posts 27
Points 435

Hehehe, I'll do it in C#

string lblTextBox1;

int x, y;

lblTextBox1 = Label1.text;

try {

 x = int.Parse(lblTextBox1);

} catch {

 throw new Exception("duh, whatever");

}

y = x +1;

 

 

  • | Post Points: 20
Top 10 Contributor
Posts 953
Points 22,750

 why not use the Int32.TryParse() or Int64.TryParse() methods? no need to catch for exceptions.

http://devpinoy.org/blogs/cruizer
Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
  • | Post Points: 20
Top 100 Contributor
Posts 13
Points 95

I agree with cruizer. Using Convert.ToInt32() causes exceptions when trying to convert non numerical format and decimal inputs. TryParse since it returns a boolean value can work around these exceptions.

  • | Post Points: 5
Page 1 of 1 (7 items) | RSS

Copyright DevPinoy 2005-2008