VB Polymorphism Constructors Default and Propertie

2019-07-31 16:29发布

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!

2条回答
贼婆χ
2楼-- · 2019-07-31 16:38

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.

查看更多
【Aperson】
3楼-- · 2019-07-31 16:39

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)
查看更多
登录 后发表回答