Sigenes
Sigenes

Reputation: 495

Make a Class property both Default AND Shared

I have a class called "PartTypes" which contains a Shared List of "PartType" objects. I have an "Item" property inside the PartTypes class that retrieves a PartType in the shared list by name.

In my main code, I want to be able to say something like PartTypes("ItemX") rather than PartTypes.Item("ItemX"). However, I can't figure out how to make the shared "Item" Property also be the default Property for my class.

Here's a simplified and condensed version of what I want to do, using a list of String rather than a list of 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

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

In case you're wondering why I would want to do it like this, here's an extended version that should give a better idea of how I'm actually using the PartTypes class (but you don't need to digest all of this unless you want to -- a solution that works for the simplified version above will probably work for the actual thing):

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

Upvotes: 0

Views: 121

Answers (1)

JohnyL
JohnyL

Reputation: 7122

You cannot have Shared default property on type itself - it must be an instance of that class. In order to make Item property default, add Default to the property:

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

Usage:

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

Upvotes: 2

Related Questions