What to use instead of collections?

philprice

Centurion
Joined
Mar 21, 2003
Messages
116
Location
Hull, UK
Ive just been looking through the object browser and collections are a member of Microsoft.VisualBasic is there a system object that has the same methods (or similar etc) to the VB Collection object?
 
ArrayList is pretty much the most general-purpose collection class in the framework, I'd say. The HashTable is useful if you need a collection of name-value pairs.

You are encouraged to inherit from CollectionBase to make your own strongly-typed collections :)
 
If you want an object that works like VB6's Collection, then a Hashtable is very similar, assuming you want to put objects in and retreive them by a Key. .NET supports many types of "collections", such as a Stack, a Queue, the ArrayList, Hashtable, and more. Plus, as divil mentioned, you can create your own collection objects that are type-safe.

-Nerseus
 
I just want the a structure so i can do

For each objectType in collection
..
Next

I'll look in to the options later on today. Thanks guys.
 
philprice said:
I just want the a structure so i can do

For each objectType in collection
..
Next

I believe it's the IEnumberable interface which supports this, so anything that implements that interface supports for each. What I'm getting at, is that you can do a for each with a basic array, you don't need a collection.
 
But i want to use collections as the .add and .remove methods are really useful along with .count because i have a collection of classes with collections inside, i think with arrays you have to do needless group work just to add the functionalility you want, unless arrays are treaten as objects like they are in java...
 
Everything in .NET is an object. Look up the Array object in the object browser. Instead of Count, though, the property is Length. Also an Array is not dynamic either, so obviously there is no Add or Remove method. Use Array for a static list of elements, ArrayList for dynamic list.
 
Here's an example of a static control array that Wyrd is refering to:

Public Sub HideAllBorders()
Dim ctr As Control
Dim Border As Panel
For Each ctr In Me.grpConversionTypes.Controls
If TypeOf ctr Is Panel Then
For Each Border In Borders
Border.Visible = False
Next
End If
Next
End Sub

Where Borders is the control array.
 
Actually I was just talking about regular old Arrays..

char[] someString = new char[50];

That's a static array object *shrug*

..since you mentioned it.. :) Aren't control arrays dynamic? You can add/remove controls from control arrays right?

On a side note, good example.. I forgot about control arrays and how you can loop through those too, etc. Heh.
 
Controls belong to a Control.ControlCollection collection, which is nothing more than a strongly typed collection that, as required, implements ICollection, IEnumerable, IList and ICloneable (I believe).
 
Okay ive done it with Inheriting CollectionBase, and it works nicely :), just one question, you have to overload onSet() etc, which is no problem, but you need to compare the type and thorw and error if it doesnt match, this again is okay, but the only way i could nicely get the type is..

Visual Basic:
   Private story As New RSSStory
    Private storyType As Type = Type.GetTypeFromHandle(Type.GetTypeHandle(story))

Which is okay, but im sure there is a better way of doing it??
 
Back
Top