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

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Indexers How do they work?
« on: October 22, 2016, 08:56:24 pm »
I'm trying to understand Indexers, as they seem to do what I want, but I am having a hard time understanding them.

These are described in section 8.1.4 Indexers page 56 and 57 of the manual. The example is only partial code however, and with multiple references to "lists" and "foo", but no example of the class FooList that "operates as a list of Foo classes" (sorry that statement just disappears up its own foo list), I cannot figure it out.

Does anyone have a full example of how Indexers work? Preferably one without foo or bar, so that I can understand them?

I'm trying to create a c like structure, that I can iterate on in a for loop, The structure would have elements of different types. I could use a class possibly as a structure, and was thinking to use Indexers to index it.

Maybe I'm going about this wrong.

Any suggestions/examples?

Thanks.

Share on Facebook Share on Twitter


millennial

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Indexers How do they work?
« Reply #1 on: October 23, 2016, 07:12:48 pm »
I don't have an example, but maybe this short explanation will help.

In the example on page 56 and 57 there is a FooList that possibly extends the Base List class. In order to act as a List, FooList must implement the behavior of all of the List's functions/methods/subroutines. Lets rename "Foo" to "Car." The integer indexer is pretty simple for CarList, but the string indexer may be complicated. How do you find (index) a Car? By brand, year, model, or color? Your implementation of the signature below will define that.

Code: [Select]
Public Function Item(index As String) As Car

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Indexers How do they work?
« Reply #2 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 »

mc-Abe

  • Full Member
  • ***
  • Posts: 167
    • View Profile
    • mc-Things
Re: Indexers How do they work?
« Reply #3 on: October 24, 2016, 10:53:06 am »
It appears there is an issue with the Remove function on lists. I think I have tracked it down. I will verify this further and we will release a new version of mcMod120 soon to address this and a few other issues.


Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Indexers How do they work?
« Reply #4 on: October 24, 2016, 11:02:39 am »
Thanks!

This is actually on module 110, so I need that most...

I had come to the same conclusion, remove on lists does not work (or I was going crazy) - or both.

I wrote my own remove method, and it worked!

At least my understanding of the universe is not flawed. It's the universe that is flawed...
« Last Edit: October 24, 2016, 11:33:12 am by Nick_W »
Like Like x 1 View List

mc-Abe

  • Full Member
  • ***
  • Posts: 167
    • View Profile
    • mc-Things
Re: Indexers How do they work?
« Reply #5 on: October 24, 2016, 11:49:22 am »
Haha! No problem Nick. That functionality is shared between both mcMod110 and mcMod120 so as you mentioned the problem is in both. We will release both versions simultaneously.

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Indexers How do they work?
« Reply #6 on: October 31, 2016, 07:44:13 pm »
Thanks for the new versions. Remove now works, but InsertAt doesn't.

Just FYI.

mc-Abe

  • Full Member
  • ***
  • Posts: 167
    • View Profile
    • mc-Things
Re: Indexers How do they work?
« Reply #7 on: November 02, 2016, 12:14:51 pm »
I will have a look at this and have a fix release soon.

mc-Abe

  • Full Member
  • ***
  • Posts: 167
    • View Profile
    • mc-Things
Re: Indexers How do they work?
« Reply #8 on: November 02, 2016, 01:32:40 pm »
How do you expect InsertAt to work? According to the documentation it inserts an element at the specified index. It shifts everything else. This seems to be consistent with my tests. Are you expecting InsertAt to overwrite the element at the given index?

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Indexers How do they work?
« Reply #9 on: November 02, 2016, 02:29:55 pm »
No, I'm trying to use mylist.InsertAt(0, myObject), to insert an object at the top of the list (as opposed to adding it to the bottom), but when I try to compile the program, I get an "Object reference not set to an instance of an object" compiler error. I can Add the same object, just not InstertAt.

I wrote my own InsertAt routing to work around this, (which works fine), but I figured it must be another bug. This is using mc-Studio 0.7-903.

mc-Abe

  • Full Member
  • ***
  • Posts: 167
    • View Profile
    • mc-Things
Re: Indexers How do they work?
« Reply #10 on: November 03, 2016, 11:41:24 am »
I have found the problem. Will be fixed in the next mcStudio release.

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Indexers How do they work?
« Reply #11 on: November 03, 2016, 11:43:43 am »
Just call me the bugfinder...
Like Like x 1 View List