制作 Class 属性 默认和共享

Make a Class property both Default AND Shared

我有一个名为 "PartTypes" 的 class,其中包含 "PartType" 个对象的共享列表。我在 PartTypes class 中有一个 "Item" 属性,它按名称检索共享列表中的 PartType。

在我的主要代码中,我希望能够说 PartTypes("ItemX") 而不是 PartTypes.Item("ItemX")。但是,我无法弄清楚如何使共享 "Item" 属性 也成为我的 class.

的默认 属性

这是我想要做的事情的简化和浓缩版本,使用字符串列表而不是 PartType 列表:

Sub MainCode
    'How I have to do it now:
    oPartType = PartTypes.Item("Type1")

    'How I'd like to do it:
    oPartType = PartTypes("Type1")
End Sub

Class PartTypes
    Private Shared _PartTypes As New List(Of String)

    'Initialize global list of PartTypes:
    Shared Sub New
        _PartTypes.Add("Type1")
        _PartTypes.Add("Type2")
    End Sub

    'Property I want to be the "default":
    Public Shared ReadOnly Property Item(Name As String) As String
        Get
            If _PartTypes.Contains(Name) Then
                Return Name
            Else
                Return ""
            End If
        End Get
    End Property
End Class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~

如果您想知道我为什么要这样做,这里有一个扩展版本,可以让您更好地了解我实际上是如何使用 PartTypes class(但您不知道除非您愿意,否则不需要消化所有这些内容——适用于上述简化版本的解决方案可能适用于实际情况):

Function SetUpType(TestTypeName As String) As PartType
    If PartTypes.IsType(TestTypeName) Then
        Dim oPartType As PartType

        'How I have to get the PartType object:
        oPartType = PartTypes.Item(TestTypeName)

        'How I'd like to get the PartType object:
        'oPartType = PartTypes(TestTypeName)

        'Set up oPartType:
        '...

        Return oPartType
    Else
        Return New PartType
    End If
End Function

Class PartType
    Public Name As String
    Public [Class] As String
    'Other properties of a PartType:
    '...
End Class

Class PartTypes
    Private Shared _PartTypes As New List(Of PartType)

    'Initialize global list of PartTypes:
    Shared Sub New
        Add("Type1","ClassA")
        Add("Type2","ClassA")
        Add("Type3","ClassB")
        Add("Type4","ClassC")
    End Sub

    Private Shared Function Add(Name As String, [Class] As String) As PartType
        Dim oPartType As New PartType
        oPartType.Name = Name
        oPartType.Class = [Class]

        _PartTypes.Add(oPartType)
        Return oPartType
    End Function

    'Property I want to be the "default":
    Public Shared ReadOnly Property Item(Name As String) As PartType
        Get
            For Each oPartType As PartType In _PartTypes
                If oPartType.Name = Name Then Return oPartType
            Next

            'If Type not found...
            Return New PartType
        End Get
    End Property

    'Examples of other PartTypes functions:
    Public Shared Function IsType([TypeName] As String) As Boolean
        For Each oPartType As PartType In _PartTypes
            If oPartType.Name = [TypeName] Then Return True
        Next

        'If Type not found...
        Return False
    End Function
End Class

类型本身不能有 Shared 默认值 属性 - 它必须是 class 的一个实例。为了使 Item 属性 默认,将 Default 添加到 属性:

Class PartTypes
    Default Public ReadOnly Property Item(Name As String) As String
        Get
            '...
        End Get
    End Property
End Class

用法:

Dim pt = New PartTypes()
Dim x = pt("Type1")