Option Base 0 or Option Base 1?

What is your preference?


  • Total voters
    2

Lokai

Board Regular
Joined
Nov 3, 2003
Messages
73
I prefer Option Base 1.
It makes transfer from array-to-sheet and vice-versa less complicated. Option Base 0 is the default but I don't see the practical advantage.
?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
For me the answer is consistency and not getting confused over what has been set as base 1 and what hasn't. Lots of controls with multiple items (e.g. listboxes, comboboxes) start at zero. Any library you refer to will probably have collections and these are likely to always start at zero. If I know that I should always start at zero then I'm less likely to make a mistake when writing code.

Anyway, I don't think there's a right or a wrong way, it's one of those things which is down to personal preference :)
 
Yeah, 0-based seems to be "in". Wonder why though. The theory of a zero-th unit perhaps? Really don't know. Personally, I prefer base=1 so First=1. Just seems easier to explain to people... 'course, then you've still got to explain why the variable "i" has a value of 11 after --

For i=1 to 10
Next i

:LOL:
 
Don't think 0 is "in", it's always been like that. I think it boils down to binary (yawn). With 4 bits for example you can represent a number up to 15 (1+2+4+8). If we count from 1 then that gives us 15 usable numbers but if we count from zero it gives us an extra number free! That's 16 different memory addresses the computer can look at, or 16 bytes of information that can be written to a disk at a time, blah blah. A poor analogy would be giving a postman who's strong enough to carry 16 parcels a bag only big enough to fit 15 in. If he has 16 parcels to deliver then he's going to have to make a trip back to the depot to get his last parcel and he won't be a happy postman.

I promise I won't make another analogy on this board.
 
My answer is neither, I simply use the default, whether it's 1 or 0 on an array, never having touched the Option Base declaration. If I don't know where the default is, I tend to test the lower and upper boundaries while I'm developing. One trick, say with Resize() could be to add:

(lbound(myArr)-1)*-1

For a consistent 1-base effect.
 
Always explicit declaring Option Base 0 or 1 is good coding :biggrin:

If You have 0 apples in real life it means You have no apples but this is not real life and it´s not about how many at all :wink:

If we have 4 apples we can name them in some kind of order like first, second... 0 or 1 based is used to index arrays. The use of 0-based indexing refers to assembly language and all the hardware starts with 0-based addresses.

So when JPG mention that VB.NET push for 0-based it means that MS accept the way things are in the computer-world :LOL:

There shouldn´t be a need to check the LBound for arrays if we know the basic about arrays in XL. Reading data into arrays from sheets will always return a 1-based array even if we explict states Option Base 0 as the below case shows.

Option Explicit
Option Base 0

Sub Read_Data()
Dim rnData As Range
Dim vaData As Variant
Dim i As Long

Set rnData = Range("A1:A4")

vaData = rnData.Value

'Wrong way
For i = 0 To 3
'This will erase runtime error 9 - subscription out of range
'as the index 0 does not exist
Debug.Print vaData(i, 1)
Next i

'Right way
For i = 1 To 4
Debug.Print vaData(i, 1)
Next i

End Sub


Personally I use 0-based and 1-based where they are the most suitable. As the case shown above it is sometime difficult to use 0-based.

So where is the third option based on index start at 1 and second option based on indext start at 0.

I promise I won't make another analogy on this board.
You do :ROFLMAO:

Take care,
Dennis
 

Forum statistics

Threads
1,214,548
Messages
6,120,146
Members
448,948
Latest member
spamiki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top