Custom Collections

J

James T.

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
 
W

William F. Robertson, Jr.

How about running a int TotalItemAdded private field?

private int TotalItemsAdded

public Add( object o )
{
o.ID = "item_" + TotalItemsAdded.ToString();
//Add item
TotalItemsAdded++;
}

Note that nothing decrements TotalItemsAdded.

bill
 
R

Roger Helliwell

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?

Guid guidID = Guid.NewGuid();

Roger
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top