labisem Posted June 5, 2003 Posted June 5, 2003 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) Quote
*Gurus* divil Posted June 5, 2003 *Gurus* Posted June 5, 2003 Early binding doesn't stop you, it just means you have to cast your variables explicitly: DirectCast(a(0), Employee).MyField Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Heiko Posted June 5, 2003 Posted June 5, 2003 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 :-) Quote .nerd
Administrators PlausiblyDamp Posted June 5, 2003 Administrators Posted June 5, 2003 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Administrators PlausiblyDamp Posted June 5, 2003 Administrators Posted June 5, 2003 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
labisem Posted June 9, 2003 Author Posted June 9, 2003 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. 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.