有没有更简单的方法来处理基本类型列表

时间:2019-11-15 18:28:34

标签: vb.net list generics narrowing

我有一个基类List(Of Person)的列表,其中包含变量,这些变量是子类EmployeeManager

我想在列表中的每个人上调用SetName,但是vb.net不会选择最窄的重载SetName函数。有没有办法自动自动选择最窄的SetName函数?这是我的示例代码

Module Example
    Class Person
        Public Name As String
    End Class

    Class Employee
        Inherits Person
        Public EmployeeID As String
    End Class

    Class Manager
        Inherits Person
        Public ManagerID As String
    End Class

    Sub Main()
        Dim PersonList As New List(Of Person)
        Call PersonList.Add(New Employee)
        Call PersonList.Add(New Manager)

        For Each CurrentPerson As Person In PersonList
            Call SetName(CurrentPerson)
        Next
    End Sub

    Public Sub SetName(AnyManager As Manager)
        AnyManager.Name = "Manager Tom"
    End Sub

    Public Sub SetName(AnyEmployee As Employee)
        AnyEmployee.Name = "Employee Bob"
    End Sub

    Public Sub SetName(AnyPerson As Person)
        AnyPerson.Name = "Person Jim"
    End Sub
End Module

列表中的每个人最终都以“ Person Jim”结尾,但是我希望第一个Employee是“ Employee Bob”,第二个Manager是“ Tom Manager”

这是最干净的方法吗?这是我可以想到的唯一方法。就我而言,这将变得很混乱,因为我有大约20个派生类,而不是2。

For Each CurrentPerson As Person In PersonList
    If TypeOf (CurrentPerson) Is Employee Then
        Call SetName(CType(CurrentPerson, Employee))
    ElseIf TypeOf (CurrentPerson) Is Manager Then
        Call SetName(CType(CurrentPerson, Manager))
    End If
Next

编辑:我认为我未能使我的需求更加明确。 Setname只是要作为任何需要列表中窄类型而不是基类的函数的替代。实际名称并不重要。也许我应该使用一个示例,其中该函数实际上与员工ID和经理ID混淆。抱歉,不清楚。

1 个答案:

答案 0 :(得分:0)

不确定所要查找的内容,但以下代码获取了您要的名称。我摆脱了SetName子对象,并在类的构造函数中设置了name属性。您可以使参数化的构造函数设置不同的名称。顺便说一句,在大多数情况下,不需要Call语句。

Private Sub OPCode()
    Dim PersonList As New List(Of Person)
    PersonList.Add(New Employee)
    PersonList.Add(New Manager)

    For Each CurrentPerson In PersonList
        Debug.Print(CurrentPerson.Name)
    Next
End Sub

Class Employee
    Inherits Person
    Public EmployeeID As String
    Public Sub New()
        Name = "Employee Bob"
    End Sub
End Class

Class Manager
    Inherits Person
    Public ManagerID As String
    Public Sub New()
        Name = "Manager Tom"
    End Sub
End Class

Class Person
    Public Name As String
    Public Sub New()
        Name = "Person Jim"
    End Sub
End Class

使用您的SetName子代码并且没有参数化的构造函数的代码...

Private Sub OPCode()
    Dim PersonList As New List(Of Person)
    Dim e As New Employee()
    SetName(e)
    PersonList.Add(e)
    Dim m As New Manager()
    SetName(m)
    PersonList.Add(m)

    For Each CurrentPerson In PersonList
        Debug.Print(CurrentPerson.Name)
    Next
End Sub

Public Sub SetName(AnyManager As Manager)
    AnyManager.Name = "Manager Tom"
End Sub

Public Sub SetName(AnyEmployee As Employee)
    AnyEmployee.Name = "Employee Bob"
End Sub

Public Sub SetName(AnyPerson As Person)
    AnyPerson.Name = "Person Jim"
End Sub

Class Employee
    Inherits Person
    Public EmployeeID As String
End Class

Class Manager
    Inherits Person
    Public ManagerID As String
End Class

Class Person
    Public Name As String
End Class

两个代码都会产生...

Employee Bob
Manager Tom

在立即窗口中