Results 1 to 2 of 2

Thread: Collection Index vs every other index I can think of **Resolved **

  1. #1

    Thread Starter
    Hyperactive Member SjR's Avatar
    Join Date
    Jul 2001
    Location
    Birmingham, UK
    Posts
    336

    Angry Collection Index vs every other index I can think of **Resolved **

    Why?
    Why aren't collection's indexes zero based?

    I have a list of 'instances' in a combo box
    The 'instances' are members of a collection.

    Item 1 in the combo box has an index of 0
    But.. has an index of 1 in the collection... Pants!
    It would be nice to write:-
    clsObjects.Item(Me.ComboBox1.SelectedIndex).DoSomething
    There are arrays and other controls that link to this collection via the index (or would do if I didn't have to keep remembering to put Index+1)

    Does anyone know how I can get round this nicely?
    Option 1 - Put something else in the combobox (and arrays etc..) at index 0 and start from 1.
    Option 2 - ...
    Thats as far as I got.
    Anyone else had this problem?
    Last edited by SjR; Jan 22nd, 2003 at 09:17 AM.
    Another satisfied customer

  2. #2

    Thread Starter
    Hyperactive Member SjR's Avatar
    Join Date
    Jul 2001
    Location
    Birmingham, UK
    Posts
    336
    The answer is:
    The "Item" function that you create in the Collection class to refer to the appropriate instance can reference the item by Index (Base 1 ) or by Key (specified when the instance is added to the collection).

    I couldn't get the parameter of the built-in "Item" function to reference the Key and NOT the index - even though I was passing the value as an Object and not an Integer. (There are two syntax versions of the Item Function - passing Index as Integer to reference the INDEX, or passing Index as Object to reference the KEY)

    Anyway, the answer is:
    VB Code:
    1. Public Function Item(ByVal Key As [color=red]String[/color]) As AddressLibrary
    2.     Return colLibraries.Item(Key)
    3. End Function

    and NOT...

    VB Code:
    1. Public Function Item(ByVal Key As [color=red]Object[/color]) As AddressLibrary
    2.     Return colLibraries.Item(Key)
    3. End Function

    Someone somewhere may appreciate this one day
    Another satisfied customer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width