Integer Vs Long Confusion

前端 未结 7 1159
小鲜肉
小鲜肉 2020-11-27 18:49

I have seen many believe in the following

VBA converts all integer values to type Long

In fact, even the MSDN article says

7条回答
  •  不知归路
    2020-11-27 19:34

    The conversion is only for memory optimization, not for user code. For the programmer, there is practically no change since the min/max limits of datatypes remain the same.

    If you take that para as a whole, you will realize that that statement is in context of performance only, and not otherwise. This is because the default size of numbers is Int32 or Int64 (depending on whether it is 32-bit or 64-bit system). The processor can process upto that big number in one go. If you declare a smaller unit than this, the compiler has to downsize it, and that needs more efforts than simply using the default type. And the processor has really no gain either. So even though you declare your variable as Integer, the compiler allocates it a Long memory, because it knows that it has to do more work without any gain.

    As a VBA programmer what is of significance for you is – Declare your variables as LONG instead of INTEGER even if you want to store small numbers in them.

提交回复
热议问题