Internal allocation VB
Let's say I have any array Trial() As Integer
Dim Left As Integer
Dim Right As Integer
Now I am increasing the array index of trial
ReDim Preserve Trial(Left+Right)
Now If My total (Left+Right) exceeds Integer limit the above will give error.
And if redeclared Left as Long then 开发者_JAVA技巧it will work fine.Actually I want to understand the internal calculation for (Left+Right).
Does it allocate the space for total depending on the datatype of "Left"? Or it may also depends on datatype of "Right"?It depends on both. The compiler will examine both variables and determine from both what data type it needs. For example. If you were to add (or multiple or divide) an Integer and a Long then the result will give you a long.
The calculation Left + Right
is done presuming the result was an integer too, that's where the overflow occurs.
If you go CLng(Left) + CLng(Right)
first, it is done the same way, only that the result will be a long, and thus no overflow occurs.
精彩评论