Custom Collections

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James T.

    Custom Collections

    Hello!

    I defined a custom collection class that inherits from CollectionBase, it
    works fine...

    Now, I would like to add each item an ID property, so I could access items
    in the collection by ID. What I need is a sample how I can generate unique
    ID each time when a new Item is added to the collection?

    Thanks!

    James


  • Oenone

    #2
    Re: Custom Collections

    James T. wrote:[color=blue]
    > Now, I would like to add each item an ID property, so I could access
    > items in the collection by ID. What I need is a sample how I can
    > generate unique ID each time when a new Item is added to the
    > collection?[/color]

    The way I approached this is to use actual values from within the collected
    objects to provide IDs.

    If I'm collecting users, I'll use the username as the identifier. This has
    to be unique as each user has a unique username.

    I then get my Item property to scan through the collection looking for the
    user with the supplied username. If it finds it then the user is returned,
    if no user is located then Nothing is returned. This also gives me a really
    easy 'Exists' type function, because if Item returns Nothing then the item
    doesn't exist within the collection.

    It also has the advantage of dynamically updating the item keys if the
    properties of the objects change. If I modify an object's Username property,
    the Item key will automatically be updated with it (as they are both the
    same value).

    This may or may not be practical depending on what type of object you are
    collecting, but worth thinking about.

    --

    (O)enone


    Comment

    • Cor Ligthert

      #3
      Re: Custom Collections

      James,

      Are you sure that you cannot use a GUID



      I hope this helps,

      Cor


      Comment

      • James T.

        #4
        Re: Custom Collections

        Thank you for good suggestion...

        James
        [color=blue][color=green]
        >> Now, I would like to add each item an ID property, so I could access
        >> items in the collection by ID. What I need is a sample how I can
        >> generate unique ID each time when a new Item is added to the
        >> collection?[/color]
        >
        > The way I approached this is to use actual values from within the
        > collected objects to provide IDs.
        >
        > If I'm collecting users, I'll use the username as the identifier. This has
        > to be unique as each user has a unique username.
        >
        > I then get my Item property to scan through the collection looking for the
        > user with the supplied username. If it finds it then the user is returned,
        > if no user is located then Nothing is returned. This also gives me a
        > really easy 'Exists' type function, because if Item returns Nothing then
        > the item doesn't exist within the collection.
        >
        > It also has the advantage of dynamically updating the item keys if the
        > properties of the objects change. If I modify an object's Username
        > property, the Item key will automatically be updated with it (as they are
        > both the same value).
        >
        > This may or may not be practical depending on what type of object you are
        > collecting, but worth thinking about.
        >
        > --
        >
        > (O)enone
        >[/color]


        Comment

        Working...