Declaring variables on same line fixes overflow

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Callum
    New Member
    • Nov 2012
    • 3

    Declaring variables on same line fixes overflow

    I have a program to calculate wattage from voltage and amps. If I declare V and I on different lines, then I get an overflow when V = 1000 and A = 100. If I declare them on the same line, then the overflow is fixed and doesn't occour

    Code:
    Private Sub cmdCalc_Click()
    
    Dim W As Long
    Dim Amps As Integer
    Dim Voltage As Integer
    
    Voltage = CInt(txtVolts.Text)
    Amps = CInt(txtAmps.Text)
    
    If Voltage >= 1 And Voltage <= 1000 Then
        If Amps >= 1 And Amps <= 100 Then
            W = CLng(Voltage * Amps)
            txtWatts.Text = W
        Else
            MsgBox "Enter amps between 1 and 100"
        End If
    Else
        MsgBox "Enter volts between 1 and 1000"
    End If
    
    End Sub
    Last edited by Rabbit; Nov 14 '12, 06:47 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    Show us your declaration when it's on one line.

    And is this VBA or VB? Because this is the VB forum and I suspect you're using VBA.

    Comment

    • Callum
      New Member
      • Nov 2012
      • 3

      #3
      Code:
      Dim Voltage, Amps As Integer
      This is VB6.

      Comment

      • lexlythius
        New Member
        • Nov 2012
        • 2

        #4
        Callum, this line
        Code:
        Dim Voltage, Amps as Integer
        is equivalent to
        Code:
        Dim Voltage as Variant
        DIm Amps as Integer
        Not to this, as one might think
        Code:
        Dim Voltage as Integer
        DIm Amps as Integer

        Comment

        • lexlythius
          New Member
          • Nov 2012
          • 2

          #5
          Overflow does not occur because being Voltage a Variant, it becomes a Long which has an upper limit of (2^31)-1, whereas Integer upper limit equals (2^15)-1

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Lexly is absolutely correct. On one line, the equivalent would be:
            Code:
            Dim Voltage as Integer, Amps as Integer
            Which will result in an overflow. Just declare them as Longs.

            Comment

            • Callum
              New Member
              • Nov 2012
              • 3

              #7
              Didn't realise the variable defaulted to variant when done one line. Thanks

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Variables always default to Variant type, unless you specify a type. The important point here is not that they were on a single line, but that you didn't specify a type for the first one.

                You would get the same effect if you coded
                Code:
                Dim Voltage
                Dim Amps As Integer

                Comment

                Working...