Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a class. Let's say Class Employee. I want to store the instances of this class to a container. A simple solution is an array (Dim a(10) as New Employee), but I want to handle the container dynamic at runtime (for example, to add an instance to the container with a user click).

Which is the best solution?

I've tried Arraylist:

 

Dim a As New Arraylist

a.Add(Emp1 As New Employee)

 

but early binding (Option Strict On) doesn't allow me to handle the instance (for example, a(0).Age)

Posted

I'm a great fan of collections myself.

 

I'd build a collection class.

In the only constructor this class will receive a type obejct, against which all futute "add" calls will be checked.

 

Thats about all. The rest is simply programming a wrapper around a normal collection.

 

Oh, and probably divil knows a built-in class that does all that :-)

.nerd
  • Administrators
Posted

Generally I would inherit from one of the Base classes under System.Collections.

IIRC CollectionBase for collections or DictionaryBase for a name / value pair.

 

It is a fairly simple matter to then provide your own type safe add / remove / item overloads that only deal with the class / interface you desire.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Administrators
Posted

A bit rough and ready (no error trapping for example) - but should give you a rough idea

 

Public Class Employee
   Public EmployeeName As String
   Public EmployeeID As Integer
End Class

Public Class EmployeeCollection
   Inherits CollectionBase

   Public Function Add(ByVal emp As Employee) As Integer
       Return InnerList.Add(emp)

   End Function

   Public Function Remove(ByVal emp As Employee)
       innerlist.Remove(emp)
   End Function

   Public Function RemoveRange(ByVal index As Integer, ByVal count As Integer)
       innerlist.RemoveRange(Index, Count)
   End Function

   Default Public Property Item(ByVal index As Integer) As Employee
       Get
           Return DirectCast(innerlist.Item(index), Employee)
       End Get
       Set(ByVal Value As Employee)
           innerlist.Item(index) = Value
       End Set
   End Property

End Class

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
Thank you all. You were very helpfull. I think that the custom collection it's the best solution. The example from PlausiblyDamp was very good.

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