|
-
Jan 24th, 2005, 11:25 AM
#1
Thread Starter
Junior Member
Overflow Error:
I am casting from string to int using both the Val and CLng functions. The program must deal with 6 digit numbers and I was under the impression that CLng could handle these but I am getting the error,
Heres the code:
If txtRentalPrice.Text = "" Then
intRentalPrice = "0"
Else: intRentalPrice = CLng(Val(txtRentalPrice.Text))
End If
Any ideas? Thanks Mark
-
Jan 24th, 2005, 11:38 AM
#2
Re: Overflow Error:
It worked fine for me. You said you were dealing with six digit numbers so I used 999999 and had no problems.
I do have one question though. Why is intRentalPrice (which implies integer, lng implies a Long) being set to a string? "0" is string...0 is an integer. Here is what I tested
VB Code:
Private Sub Command1_Click()
Dim lngRentalPrice As Long
If txtRentalPrice.Text = "" Then
lngRentalPrice = 0
Else
lngRentalPrice = CLng(Val(txtRentalPrice.Text))
End If
MsgBox lngRentalPrice
End Sub
-
Jan 24th, 2005, 11:45 AM
#3
Thread Starter
Junior Member
Re: Overflow Error:
Ahh
I was declaring the variable as type Integer rather than Long!
Thanks for your help Hack
-
Jan 24th, 2005, 11:47 AM
#4
Hyperactive Member
Re: Overflow Error:
As Hack said, it should easily handle any 6-digit value. In fact, it's good up to any 9-digit value... Unless the value in the TextBox ends with a "%" (e.g., "999999%"). In such a case, Val will try to convert to an Integer - causing an overflow.
As an aside, the whole IF check is unnecessary as the Val function will convert "" to zero. All you need is:
VB Code:
lngRentalPrice = CLng(Val(txtRentalPrice.Text))
no conditional check for "" is required.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|