loco3424
loco3424

Reputation: 23

Need help understanding this this function

Dim i as Long, arr(5) As Long

for i = 1 to 6
    arr(i-1) = i-1
next 
console.writeLine(arr(LBound(arr)+5) + arr(UBound(arr)-2))

I understand the output is 8 but could someone explain why it is 8, Lbound and UBound, this type of question comes up in my exam, and i am having some issue with getting my head around this.

Much appreciated

Upvotes: -1

Views: 222

Answers (1)

Stephen Wrighton
Stephen Wrighton

Reputation: 37819

UBOUND means "Upper Boundary" in VB. It returns the sequence number for the last item (the upper boundary) of an array. LBOUND is for the lower boundary or first item.

I don't typically use them these days as VB.Net has easier to use options to do the same thing.

your loop builds this array

arr(0)=0 
arr(1)=1 
arr(2)=2 
arr(3)=3 
arr(4)=4 
arr(5)=5

Now LBound(arr) here returns 0, and the ubound(arr) returns 5.

now, if you array was this:

arr(0)=10
arr(1)=11
arr(2)=12
arr(3)=13
arr(4)=14
arr(5)=15

Then LBound(arr) still returns 0, and the ubound(arr) returns 5.

UBOUND FUNCTION

LBOUND FUNCTION

Upvotes: 2

Related Questions