1

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!

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

2 Answers2

0

Use the generic List(Of T) class, specialized to hold your Client 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":

Public Class Client

    Public Property Name As String

    Public Property AmountOwned As Decimal

    Public Sub Bill()
        BillingManager.BillClient(Me)
    End Sub

    ' ... etc.

End Class  

Then, you would create the List(Of T) to hold all of your instances of the Client class:

Dim clients As New System.Collections.Generic.List(Of Client)

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 using List(Of T), you would inherit from Collection(Of T) and create a custom collection class like so:

Public Class ClientCollection
    Inherits System.Collections.ObjectModel.Collection(Of T)

    ' ... customize as desired ...

End Class

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.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Hi Guys, all went well up until the end... In the transactions class I need to be able to increase or decrease the clients balance. Any ideas how I go about referencing the client from the nested transactions class? Thanks again –  Feb 21 '12 at 03:15
0

Create a Transaction and a Client class

Public Class Transaction
    'TODO: Implement Transaction
End Class

Public Class Client
    Public ReadOnly Transactions As List(Of Transaction) = New List(Of Transaction)

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub

    Public Name As String
End Class

Create a ClientList class

Public Class ClientList
    Inherits List(Of Client)

    Public Overloads Sub Add(ByVal name As String)
        Add(New Client(name))
    End Sub

    Public Sub Refresh()
        ' Do what ever you want Refresh to do
    End Sub
End Class

You can then use the client list like this

Dim clients As New ClientList

clients.Add("S")
' Or
clients.Add(New Client("T"))

Dim n As Integer = clients.Count
Dim m As Integer = clients(0).Transactions.Count

clients.Refresh()
clients.RemoveAt(5)
clients(n - 1).Transactions.Add(New Transaction())
clients(n - 1).Transactions.RemoveAt(2)
Dim name As String = clients(0).Name
Dim client As Client = clients(0)
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • You really shouldn't inherit from `List(Of T)`. It wasn't designed to be inherited from, and the `Add` method (among others) is not virtual (overridable). The recommended approach is either to inherit `Collection(Of T)`, or implement the `IList(Of T)` interface. – Cody Gray - on strike Feb 16 '12 at 02:15
  • I don't override it, I overload it, making it possible to add a customer by just specifying a customer name, instead of adding a customer object. If `List(Of T)` wasn't designed to be inherited from, then it would have been made `NotInheritable`. – Olivier Jacot-Descombes Feb 16 '12 at 02:26