[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Variable is Int64 type but get error System.OverflowException

Hi

I'm trying to do:

        Dim MyNumber As Integer
        Dim Long_Number As Int64
        MyNumber = 407,414
        Long_Number = MyNumber * 1024 * 1024
      ' Result is : 437,690,302,464
      
But got this error:

      System.OverflowException: Arithmetic operation resulted in an overflow.

It is not supposed that a long type number can store up to 9,223,372,036,854,775,807 ?

Also I try to do this:

        Long_Number = CType(MyNumber * 1024 * 1024, Int64)

But got this error: Long_Number = 0
0
Desperate34
Asked:
Desperate34
  • 2
1 Solution
 
m_larson_33Commented:
in your
Long_Number = MyNumber * 1024 * 1024
calculation, try casting the MyNumber to Int64, otherwise I suspect the right side of this equation is by default cast to Integer.  You have Integer * 1024 * 2024, which is cast to Integer, but is too big.

0
 
Bob LearnedCommented:
Try using Decimal.

Bob
0
 
m_larson_33Commented:
This works

        Dim MyNumber As Integer
        Dim Long_Number As Int64
        MyNumber = 407,414
        Long_Number = CType(MyNumber, Int64) * 1024 * 1024
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now