Home > Mobile >  SortedSet in Class does not expose correctly
SortedSet in Class does not expose correctly

Time:05-18

I have a weird problem that I can't seem to figure out. Even weirder is that I'm fairly sure it has worked in the past, but not anymore.

I have a class where I define a variable as SortedSet. In a function, I can reference the variable, but its SortedSet attributes are not exposed. If I use them anyway, some of them work, others don't. If I create that variable inside my function, all works as expected.

This is the code:

Public Class MyTest
    Public MySortedSet = New SortedSet(Of String)()

    Public Sub New()
        Dim MySortedSet2 = New SortedSet(Of String)()


        'Constructor. To use this, add         Dim MyTest As MyTest      to the Form1_load sub.
        Me.MySortedSet.add("Test")
        For Each Item In Me.MySortedSet

            MsgBox(Item)    'This does print Test
        Next Item

        Me.MySortedSet.add  '.add not exposed
        MySortedSet2.add    '.add is exposed

    End Sub

End Class

See the screenshot below. The first example only has 4 items, where the 2nd example has a full list of parameters. I need to fix this using the first example, so the ElementAt one works. It works in the second example, but not in the first. It gives the error that ElementAt is not part of this object.

enter image description here

How can I get the full list of parameters for me.MySortedSet.??????

CodePudding user response:

You should declare MySortedSet as a Property:

Public Class MyTest

    Public Property MySortedSet As New SortedSet(Of String)()

    Public Sub New()

        Dim MySortedSet2 As New SortedSet(Of String)()

        Me.MySortedSet.Add("Test")

        For Each item As String In Me.MySortedSet
            Debug.WriteLine(item)
        Next item

        Me.MySortedSet.Add("Test")
        MySortedSet2.Add("Test")

    End Sub

End Class

You should also indicate variable types every time you declare a variable, even in a For Each statement.

  • Related