-
Jul 23rd, 2008, 01:39 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Overflow: 'Cint': Simple Cint Question?
If I recieve nonnumeric input on the following lines of below:
Code:
strTempBoxQty = Mid(Trim(Scan7.Text), 2, Len(Scan7.Text) - 1)
'**********************************************************************
If IsNumeric(CInt(strTempBoxQty)) = False Then
Call beep
strWhichScanControl = "Scan7"
frmDialog.Label1.Caption = "Non-numeric input supplied."
frmDialog.Show
Scan7.Text = ""
Exit Sub
'blnScanError = True
End If
Then I will receive the following error message:
"An error was encountered while running this program: Overflow: 'Cint'"
Rather thanrecieving the error message I coded.
This is frustrating as I would like to see my error message. Could this have anything to do with how I am using Cint() and IsNumeric()?
Any help would be greatly appreciated! Thanks!
Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.
-
Jul 23rd, 2008, 01:43 PM
#2
Re: Overflow: 'Cint': Simple Cint Question?
Use
Code:
If Not IsNumeric(strTempBoxQty) Then
Also, do not declare this variable As String.
Declare it As Long or As Integer or something and you won't ever have to worry about whether it contains numbers or not.
-
Jul 23rd, 2008, 01:45 PM
#3
Re: Overflow: 'Cint': Simple Cint Question?
You should not be using CInt there, as it requires numeric input - and you have not yet checked if it is numeric or not. You should just have IsNumeric, ie:
Code:
If IsNumeric(strTempBoxQty) = False Then
It would be a good idea to use CInt after the code you showed, as by that point you already know that it is numeric.
edit:
Originally Posted by Hack
Also, do not declare this variable As String.
I would disagree - as it is being used to get part of a string which may or may not be numeric.
Adding an extra Integer variable would be a good idea tho, eg:
Code:
...
End If
Dim intBoxQty as Integer
intBoxQty = CInt(strTempBoxQty)
-
Jul 23rd, 2008, 02:11 PM
#4
Re: Overflow: 'Cint': Simple Cint Question?
I might add that if the value of any Scan7.Text is larger than the maximum integer limit, he will receive Error 6 Overflow when using CInt(), even if it passes the IsNumeric screening test.
-
Jul 23rd, 2008, 04:11 PM
#5
Thread Starter
Addicted Member
Re: Overflow: 'Cint': Simple Cint Question?
OK, thanks all. I thought Cint() was casting the text value as an integer so that I could test if it was numeric. I have since changed my input values to be doubles and am now using Cdbl() instead due to the integer size limitation. It is working now. Thank you all very much for your help!
Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.
-
Jul 23rd, 2008, 04:25 PM
#6
Re: [RESOLVED] Overflow: 'Cint': Simple Cint Question?
The functions like CInt etc do cast, but they will give an error if the value is not apt for the target data type (like if the number is too big, or the value is not valid [eg: "12hello"]).
The IsNumeric function is a quick check to see if the expression (which is usually a String) can be interpreted as numbers. Unfortunately it is not perfect, so you should also deal with errors (including Overflow) when converting to your target data type.
Note that as an alternative to Double, you could use Long (a bigger Integer) which allows values up to 2 billion.. but it depends on the values you are expecting.
-
Jul 23rd, 2008, 06:53 PM
#7
Re: [RESOLVED] Overflow: 'Cint': Simple Cint Question?
If the OP sticks with a Double then he will never have an overflow, IsNumeric returns false for a number beyond the range of a double.
Code:
Debug.Print IsNumeric("1.79769313486231E+308") 'within a double, returns True
Debug.Print IsNumeric("1.79769313486232E+308") 'beyond a double, returns False
At least it's worth casting to a Double to check it's range before recasting to another type.
-
Jul 23rd, 2008, 06:56 PM
#8
Re: [RESOLVED] Overflow: 'Cint': Simple Cint Question?
Originally Posted by Milk
If the OP sticks with a Double then he will never have an overflow, IsNumeric returns false for a number beyond the range of a double.
Code:
Debug.Print IsNumeric("1.79769313486231E+308") 'within a double, returns True
Debug.Print IsNumeric("1.79769313486232E+308") 'beyond a double, returns False
At least it's worth casting to a Double to check it's range before recasting to another type.
Huge!
-
Jul 24th, 2008, 03:44 AM
#9
Member
Re: [RESOLVED] Overflow: 'Cint': Simple Cint Question?
Beware of IsNumeric():
Code:
IsNumeric("0d0") 'Returns True
...because the 'e' and 'd' characters cause it to parse the string as standard form.
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
|