Creating Collections in Classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In: microsoft.public.dotnet.framework.performance
Hi, can someone give links either in msdn.microsoft.com or other sites that
tell me what are the best ways to create Collection properties in a class
written in Visual Basic.NET?

Eg., say I create a Class that instantiated allows you to get a property of
collections: Forms.Controls, TreeView.Nodes etc etc.

In my work, I only used ArrayList to create Collections but want to try
other interface classes such iCollection, hasttables, etc etc.

Thanks in advance.
 
You have to inherit from one of the collection classes you mention, and
override the methods you want... I don't know where to get a sample of this,
since I'm not in my computer right now, but there's a sample in "101 VB.Net
Samples" (if you can google it).
Here's a sample (of course, I haven't compiled it, and it will not run
unless you complete it, but it gives you an idea):

Public Class MyClass
'...Properties and Methods...
Public sub New ()
'TODO:assign default values
End Sub
Public Sub New (PropA as Integer, PropB as Integer)
'TODO:assign values to the new object
End Sub
Public Property MyPropA () as Integer
'TODO:Inner implementation of the property
End Property
Public Property MyPropB () as String
'TODO:Inner implementation of the property
End Property
End Class
Public Class MyCollection
Inherits Hashtable '(for example)
Public Overrides Overloads Sub Add (key as Object, o as object)
MyBase.Add(key, o)
End Sub
Public Overloads Sub Add (key as Object, PropA as Integer, PropB as
String)
Mybase.Add(key, new MyClass(PropA, PropB))
End Sub
'etc...
End Class

If you need a wider sample, contact me (to my e-mail) and I'll send you a
sample when I get home.
Hope this helps.
VBen.
 
Hi, VBen. Thanks a lot for your sample. That's great code.

Is there a site that describes fully the pros and cons of using which
Collection class, etc? I remember there were articles in msdn.microsoft.com
about this matter (it also include performance issues of these classes; eg.,
you should not use inherit the iComparable class if your class barely use
sorts since iComparable is an unnecessary resource hog, etc.)

Thanks.
 
I'm not aware of any site, besides MSDN. (msdn.microsoft.com or
www.msdn.com)
If you find any, please let me know, since I work with collections a lot,
inherited ones, and would like to be able to take a more accurate decission
in which base collection class to use...
Thank you.
VBen.
 
Back
Top