VB Polymorphism Constructors Default and Properties. Similar Class to Listbox

丶灬走出姿态 提交于 2020-01-05 09:15:35

问题


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!


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/9303770/vb-polymorphism-constructors-default-and-properties-similar-class-to-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!