70

How do I convert from a string to an integer? Here's what I tried:

Price = CInt(Int(txtPrice.Text))

I took out the Int and I still got an exception.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
swydell
  • 1,962
  • 8
  • 31
  • 44

8 Answers8

120

Use

Convert.toInt32(txtPrice.Text)

This is assuming VB.NET.

Judging by the name "txtPrice", you really don't want an Integer but a Decimal. So instead use:

Convert.toDecimal(txtPrice.Text)

If this is the case, be sure whatever you assign this to is Decimal not an Integer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chad Schouggins
  • 3,440
  • 2
  • 23
  • 29
  • 1
    In Vb.net..This code will get an exception 'Input string was not in a correct format.;. – Sam Casil Oct 10 '11 at 05:41
  • I just tried it but it didn't work. I got a format error this time. I'm still trying. I got almost 1 1/2 hours left, – swydell Oct 10 '11 at 05:46
  • 1
    Dim Price As Integer Dim result As Boolean = Int32.TryParse(txtPrice.Text, Price) – dan_l Oct 10 '11 at 05:58
  • 2
    The only way I can get a format error with this is to actually have an error (enter a character/symbol). Fixing this would require simple input validation which isn't stated as a requirement. – Chad Schouggins Oct 10 '11 at 06:51
  • 2
    This solution throws an error for me, and doesn't seem to be very robust for production because of this registry key issue http://support.microsoft.com/kb/942460 `CInt()` works well. – msanford Jul 04 '14 at 16:27
26

You can try it:

Dim Price As Integer 
Int32.TryParse(txtPrice.Text, Price) 
zari
  • 1,709
  • 1
  • 12
  • 18
  • 3
    I think this is really the best answer. Using Int32 or Decimal's TryParse() method would be able to handle other things being entered into the TextBox, like currency symbols and things of that nature. It also won't throw an exception, but rather just return a boolean indicating whether it passed or failed. – Seth Moore Jan 15 '14 at 16:23
13

You can use the following to convert string to int:

  • CInt(String) for ints
  • CDec(String) for decimals

For details refer to Type Conversion Functions (Visual Basic).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Srinivasan
  • 139
  • 1
  • 2
5

Please try this, VB.NET 2010:

  1. Integer.TryParse(txtPrice.Text, decPrice)
  2. decPrice = Convert.ToInt32(txtPrice.Text)

From Mola Tshepo Kingsley (WWW.TUT.AC.ZA)

Bugs
  • 4,491
  • 9
  • 32
  • 41
Moola TK
  • 51
  • 1
  • 2
3

Convert.ToIntXX doesn't like being passed strings of decimals.

To be safe use

Convert.ToInt32(Convert.ToDecimal(txtPrice.Text))
Stuart Dobson
  • 3,001
  • 4
  • 35
  • 37
2

You can try these:

Dim valueStr as String = "10"

Dim valueIntConverted as Integer = CInt(valueStr)

Another example:

Dim newValueConverted as Integer = Val("100")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Majosty D
  • 67
  • 2
1

Use Val(txtPrice.text)

I would also allow only number and the dot char by inserting some validation code in the key press event of the price text box.

Nandostyle
  • 344
  • 2
  • 12
0

If there might be invalid characters in the textbox it will throw an exception. The Val command pulls numbers and strips invalid characters. It returns a double. So you want to convert the result of Val to whatever type you need.

Price = Convert.toInt32(Val(txtPrice.Text))

This will return 0 instead of throwing an error on invalid input. If that isn't desired you should be checking that the input is valid before you convert.

HackSlash
  • 4,944
  • 2
  • 18
  • 44