Nate Bross Posted June 5, 2005 Posted June 5, 2005 I have a small class that I would like to use in a collection. Every time I recieve a connection request I want to creat a new instence of the class and put that instence in the collection. I've never worked with collections before so I don't even know where to begin, so any advice is greatly appreciated. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
thenerd Posted June 5, 2005 Posted June 5, 2005 (edited) Here's two examples, one of a collection, one of an arraylist. Both use some random class called clsClass dim a as arraylist private sub form_load(...) a = new arraylist end sub public sub connectionrequestrecieved() a.add(new clsClass) end sub [code=visualbasic] [code=visualbasic] dim c as collection private sub form_load(...) c = new collection end sub public sub connectionrequestrecieved(byref key as string) c.add(new clsClass,"key") end sub For the arraylist, you can use it just like an array for x as integer = 0 to a.count - 1 'arraylists's index's start at 0! messagebox.show(a(x).RandomProperty) next 'and for collections there's 2 ways. for x as integer = 1 to c.count 'collections start at 1! messagebox.show(c(x).RandomProperty) next 'and with collections you can access a member individually very easily with their keys. c("TheSpecialOne").RandomProperty = "f" I know you didn't ask for arraylists, but I like them better, so I felt like typing them as well. + I'm bored. Oh yeah, and TPG has a great tutorial on arraylists/stacks/collections here: http://www.vbprogramming.8k.com/tutorials/AlternativeToArrays.htm (This is his best one, I think ) Edited June 5, 2005 by thenerd Quote
*Experts* mutant Posted June 5, 2005 *Experts* Posted June 5, 2005 I would recommend creating your own collection class based on CollectionBase class. Post if you need help with that. Quote
Nate Bross Posted June 9, 2005 Author Posted June 9, 2005 Thanks for your help guys, the collection example works great. Just a question, do you use a function to make a 'key' for each object in the collection? Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
mskeel Posted June 9, 2005 Posted June 9, 2005 In short, no...that functionality is all behind the scenes stuff you get for free. In most cases, Collections are just fancy arrays or lists. If you are using a hashtable, there are two arguments in the add method, Add(key, value) -- again, you don't have to do anything special (unless you want to and then you can override the GetHashCode method of the class you are going to be adding. But you won't have to worry about that for almost everything you need to do.) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.