Why isn't 'Option Base 1' starting my array index at 1?

SAbboushi
SAbboushi used Ask the Experts™
on
I have specified 'Option Base 1' at module level, however my sub statement 'strArray = Split(strInput, ",")' results in Type 'String(0 to 3)'

How can I get my arrays to start at 1?

ANd is ther a way to define my index base on a per array basis?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
Hello SAbboushi,

The Split function always returns a zero-based array, no matter what Option Base setting you use.

You should *always* explicitly define your index base:

Dim MyArray(3 To 17) As Long
Dim YourArray(0 To 10) As Long
Dim HerArray(1 To 4, 1 To 10, 0 To 100) As String

Regards,

Patrick
Top Expert 2010

Commented:
Further, to be safe, use LBound and UBound to find the lower and upper bounds of your arrays.

Author

Commented:
Still confused.  Seems the following will not work because "The Split function always returns a zero-based array".  So how do I get this to work?

Dim strArray(1 To 4) As String
...
strArray = Split(strInput, ",")
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2008

Commented:
Dim strArray As Variant
strArray = Split(strInput, ",")

Kevin
Top Expert 2008
Commented:
This will also work:

    Dim strArray() As String
    strArray = Split(strInput, ",")

Another tip:

The Split function returns an array of strings which must be assigned to a Variant or dynamic array of Strings. The result is a zero-based one-dimensional array regardless of what Option Base is. When the Split function fails to identify any substrings it returns an empty array with a lower bound of 0 and an upper bound of -1. To test if a Split function does not return any results use the UBound function as illustrated with the sample code below.

   Dim Result As Variant
   Result = Split(Text)
   If UBound(Result) = -1 Then
      ' No values returned
   End If

Kevin

Author

Commented:
Thanks for your posts

My questions were:
(1) How can I get my arrays to start at 1?
(2) And is there a way to define my index base on a per array basis?

Patrick - you answered these questions with :
(1) The Split function always returns a zero-based array, no matter what Option Base setting you use.
(2) Dim MyArray(3 To 17) As Long

In (1), I understood you to be describing the behavior of the split function - yet I was asking about arrays.  It took Kevin's post and some experimenting to understand that what you were trying to tell me is that it is not possible for me to have a one-based array if I am using the split function to populate that array.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial