Author Topic: Indexers How do they work?  (Read 397 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Indexers How do they work?
« on: October 23, 2016, 08:07:44 pm »
Sorry, still a bit opaque, and I though c was obscure!

This is the example

Code: [Select]
03 Public Function Item(index As Integer) As foo
04    End Function
05 Public Sub Item(value As foo, index As Integer)
06 End Sub
07
08 Public Function Item(index As String) As foo
09    End Function
10 Public Sub Item(value As foo, index As String)
11 End Sub


17 Dim list As FooList = New FooList()
18    list.Add(New foo("Mike"))
19    list.Add(New foo("Dan"))
20 foo1 = list("Mike")
21 list("Dan") = foo2
22 f = list(0)
23 list(0) = f

I'm assuming that in the example "FooList" is a class. I'm assuming that the Sub and Functions defined for Item are actually part of the fooList class (which is not shown), and are overriding it's item methods.

I may be wrong though, and with no idea what FooList looks like, I'm just guessing. Also "list" is a really bad thing to use as a name for a list, given that "list" is a key word. I also don't understand what "foo" or "foo1" or "f" are either. Usually "foo" means "anything", but obviously it can't be anything, it must be a type or class, just not sure which. "foo1", "foo2" and "f" must be variables of type "foo", but don't know what "foo" is (I'm assuming another class somehow related to "FooList").

We also don't have access to the generic list(of foo) form (now I'm doing it!), just ListOfObject.

So far I have the following:

Code: [Select]
Class Test
    Shared anitem1 as String
    Shared anitem2 as String

     Public Sub New()
     End Sub

     Public Sub New(_item1 as String, _item2 as String)
         AnItem1=_item1
         AnItem2=_item2
     End Sub

     Public function item1() as String
         Return anitem1
     End function

     Public function item2()
          Return anitem2
     End function
End Class

Class Main
    Shared queue as ListOfObject

    Shared Sub additem(_item1 as String, _item2 as String)
        If queue=Nothing then
            queue = New ListOfObject()
        End if

        Queue.add(New Test(_item1, _item2))
     End Sub

      Shared Sub output()
          For each _item in queue.Clone()
              Dim item as Test = New Test()
              Item = _item.Cast(Test)
              Print(Item.item1, Item.item2). //This actually prints via MQTT
              queue.remove(_item)
           next
      End Sub
End Class

The problem is that queue.remove does not remove _item, so the list queue grows forever, reprinting all the elements in the list.

I was hoping that understanding the indexers example would help me understand what is happening here. I think it may be something to do with passing values by reference. I can't run it in the debugger, as mcStudio crashes if I try, but it does compile and run on the actual module. I just can't remove anything from the list!

Any suggestions?
« Last Edit: October 23, 2016, 08:21:11 pm by Nick_W »