Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert from a string to an integer in Visual Basic?

Tags:

vb.net

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.

like image 509
swydell Avatar asked Oct 10 '11 05:10

swydell


People also ask

How do you make an integer in Visual Basic?

You can declare and initialize an Integer variable by assigning it a decimal literal, a hexadecimal literal, an octal literal, or (starting with Visual Basic 2017) a binary literal. If the integer literal is outside the range of Integer (that is, if it is less than Int32. MinValue or greater than Int32.

Which function in VB.NET convert string to number data type?

You can use the Val function to explicitly convert the digits in a string to a number.

What command will convert Strings to integers?

In Python an strings can be converted into a integer using the built-in int() function. The int() function takes in any python data type and converts it into a integer.


2 Answers

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.

like image 189
Chad Schouggins Avatar answered Oct 26 '22 01:10

Chad Schouggins


You can try it:

Dim Price As Integer  Int32.TryParse(txtPrice.Text, Price)  
like image 40
zari Avatar answered Oct 25 '22 23:10

zari