Array and a simple variable.

D

Dave Neve

Hi

I'm trying to understand 'array' and the code below baffles me.

I would expect the answer to be '10' but it is '20'
Surely it should be 10????

Can sm please explain this?

Thanks

Sub Disarray()
Dim A As Variant, B As Variant
A = Array(10, 20, 30)
B = A(1)
MsgBox (B)
End Sub
 
P

Perry

No. Arrays declared this way are zero based. A(0)=10, a(1)=20,A(2)=30

Not if the module in question is Option Based 1, like in below sequence.
MsgBox will show 10 as value of a(1) and will fail if you want to display
a(0)

Option Base 1
Sub test()
ReDim a(2)
a(1) = 10
a(2) = 20
MsgBox a(1)
End Sub

But y're right, if the module in question doesn't have a
Option Base 1 condition, they're zero (0) based.

Krgrds,
Perry
 
A

Alex Ivanov

Correction accepted. I never used Option Base and forgot it even exists.
In rare cases I need array base other than zero, I prefer to declare them
explicitly.

Alex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top