Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer Vs Long Confusion

Tags:

I have seen many believe in the following

VBA converts all integer values to type Long

In fact, even the MSDN article says

“In recent versions, however, VBA converts all integer values to type Long, even if they're declared as type Integer.”

enter image description here

How is this possible? Consider this simple example.

Sub Sample()     Dim I As Integer     I = 123456789 End Sub 

If VBA converts all Integer values to type Long even if they're declared as type Integer, then the above should never give you the Overflow error!

What am I missing here? Or should I take it that the statement is incorrect and pay serious heed to that the link says in the beginning

like image 911
Siddharth Rout Avatar asked Nov 03 '14 15:11

Siddharth Rout


People also ask

What is the difference between long and integer?

An int is a 32-bit integer; a long is a 64-bit integer. Which one to use depends on how large the numbers are that you expect to work with. int and long are primitive types, while Integer and Long are objects.

Why do we use long integer instead of integer?

Large IntegersIf you need to hold an integer larger than the Integer data type can hold, you can use the Long data type instead. Long variables can hold numbers from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Operations with Long are slightly slower than with Integer .

What is the difference between long and integer in VBA?

Long is a data type in VBA used to store the numeric values. We know that integer also holds numeric values, but Long differs from integers as the data storage range is very big. In the case of the Long data type, we can hold decimal values too. So, it is a built-in data type.

How long is a long integer?

Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647.


1 Answers

An integer declared as an Integer is still type checked as an Integer. The msdn documentation is referencing how the variable is stored internally. On a 32 bit system, an Integer will be stored in 32 BITS not Bytes, while on a 16 bit system the value is stored in a 16 BIT space or register, it would have been stored in 16. Hence the maximum size.

There is no type conversion going on as far as VBA is concerned. An int is an int and a long is a long, even though they now take up just as much space.

like image 85
RubberDuck Avatar answered Oct 06 '22 13:10

RubberDuck