Numbers

vellaima

Centurion
Joined
Jan 29, 2003
Messages
109
Visual Basic:
Dim ij as Integer
ij = discount.Text
If ij > 100 Then
MsgBox("Cannot be greater than 100")
discount.Text = ""
discount.Focus()
End If

In the above code if ij is 100.01 i am not getting the error message. If ij is 100.60 and above i am getting the error message. Is there any other way out?
 
You need to use the Single or Double datatypes; Integer
doesn't like decimals, so 100.60 rounds to 101, 100.01 rounds to
100.

Visual Basic:
Dim ij As Single

ij = Single.Parse(discount.Text)
'etc
You might also want to add in a Try...Catch error handling
block just in case someone enters a non-number.
 
Also, go in to your project properties and turn on Option Strict. You are assigning a string directly to an integer, which is horrible. You should use Integer.Parse or the equivalent instead.

[edit]Yeah, that's what I meant[/edit]
 
Last edited:
I believe divil means Option Strict, which prevents direct conversion
from one data type to another where the possibility of data-loss
exists.
 
Back
Top