Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted (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 by thenerd
Posted
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?

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
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.)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...