Results 1 to 9 of 9

Thread: [RESOLVED] Overflow: 'Cint': Simple Cint Question?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Resolved [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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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:
    Quote 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)

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    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.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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.

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: [RESOLVED] Overflow: 'Cint': Simple Cint Question?

    Quote 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!
    Doctor Ed

  9. #9
    Member
    Join Date
    Dec 2007
    Posts
    57

    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
  •  



Click Here to Expand Forum to Full Width