Question strings as numbers

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
hi, I'm just starting out learing VB.net, and I had a question about an example from my class at school. I'm supposed to find the projected sales for a company. I have a textbox for current sales and projected increase(as a percent). My code looks like this:
me.label1.Text = me.SalesTextBox.Text * me.IncrTextBox + Val(me.SalesTextBox.Text)
That works great, but I want the user to input a number between 1-100 for the percent, not a decimal. So I need to divide the value of the
me.IncrTextBox.Text by 100. I've tried doing this, but no matter how I try I come into an error. Can somone tell me the correct Code for what I'm trying to do?
 
use a NumericUpDown control for this, and store the .Value() property in an integer for the calculations
VB.NET:
Me.Label1.Text = (CDec(Me.SalesTextBox.text) + (CDec(Me.IncrNumericUpDown.Value) * CDec(Me.SalesTextBox.Text))).ToString()
 
The following code works:-

Label1.Text = SalesTextBox.Text*IncrTextBox.Text/100+SalesTextBox.Text

you dont need to use Me btw and converting to val confuses things here
 
I use val when adding the text of the textboxes because otherwise I run into problems. For instance, If I add 4 in one text box with 2 in another, I will get 42.
 
DO NOT treat strings as numbers. All strings should be converted to numerical types before performing arithemtic operations on them. Also, TextBoxes are generally bad for numeric input and should be avoided if possible.

Using the Val function is also generally bad because if the user makes an error and enters an invalid character the calculation will proceed with a value other than what they intended. Val keeps reading characters until it finds an invalid one and just ignores everything after it.

The "proper" course of action is to validate all input to make sure that it is numeric. You would halt and point out the error to the user if there is one or else proceed. If you use a NUD as JB suggests then it handles the validation for you. If you must use TextBoxes and you're not validatin the user's input as they are making it then the "correct" thing to do is:
VB.NET:
Dim incr As Integer

If Not Integer.TryParse(Me.incrTextBox.Text, incr) Then
    'Invalid string.
    MessageBox.Show("Please enter a valid integer value from 1 to 100")
ElseIf incr < 1 OrElse incr > 100 Then
    'Invalid number.
    MessageBox.Show("Please enter an integer value from 1 to 100")
Else
    'Valid number.
    'Note that the "D" suffix is required to force the result to Decimal rather than Double.
    Dim percent As Decimal = incr / 100D

    '...
End If
 
Last edited:
I strongly suggest that you ALWAYS have Option Strict turned On unless you have a specific reason for turning it Off. You will become a better programmer more quickly if you do.
 
Thanks for your help guys. Like I said, I'm a noob, so a lot of this is going right over my head. I feel like I'm drowning in a sea of VB.net!!!
I'll figure it out though...
 
Back
Top