I've been banging my head against a wall for sometime on this one. I'm trying to create a class for storing data on People with another class to store their Bank Transactions.
Ideally, this all be hidden away and leave only simple statments, declarations and functions available to the programmer. These will include:
Dim Clients As New ClientList
Clients.Count 'readonly integer
Clients.Add("S")
Clients.Refresh()
Clients(n).Remove()
Clients(n).Transaction.Add()
Clients(n).Transaction(n).Remove()
I know this is possible as these exist in the Listbox Class though can't figure out how it's done.
Any help would be appreciated. Thanks in advance!
Use the generic
List(Of T)
class, specialized to hold yourClient
objects. It already provides all of the methods you want without your having to write a single line of code!So you would first write a
Client
class that contained all of the properties (data) and methods (actions) relating to a "client":Then, you would create the
List(Of T)
to hold all of your instances of theClient
class:If, for whatever reason, you needed to specialize the behavior of the
Add
,Remove
, etc. methods provided by the collection class, or add additional methods, you would need to change strategies slightly. Instead of usingList(Of T)
, you would inherit fromCollection(Of T)
and create a custom collection class like so:The WinForms
ListBox
class doesn't do it exactly like this because it was written before generics were introduced to the framework. But since they're here now, and you should always use them when possible, you can completely ignore how WinForms does things.Create a
Transaction
and aClient
classCreate a
ClientList
classYou can then use the client list like this