[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

nested _next for same object class, confusion


I have a question, how to implement nested key value for a class with a _next function where the key is not equal to the enum.index value.

I include a simple script to demonstrate the issue. Which happened to me by accident.

After the nested _next, the key is of course equal to the value of the last nested cycle of the inner most next.

Here a script(extremely simplified) that shows the issue I am having. Maybe I am not seeing the simple answer.

----------------------------------------------------------------------------------------
#!/usr/bin/env gbs3
' Gambas Script File Created 05/30/2024 08:36:16

Public Sub main()
Dim MyHendrix As New MyNext(6)
Print "begin"
For Each v As Variant In MyHendrix
   Print "Outer Value="; v, "Key="; MyHendrix.key
   If v = MyHendrix.count / 2 Then
     For Each vv As Variant In MyHendrix
         Print "   Inner Value="; vv, "Key="; MyHendrix.key
     Next
      Print "   Outer Value="; v, "Key="; MyHendrix.key
   Endif
Next
Print "end"
Quit 0
Catch
Error "Script Error >";; error.text & "\n" & error.where
End

Class Mynext
Private $key As Variant
Private hendrix As New Variant[]
Property Read key As Variant
Property Read Count As Integer

Private Function key_read() As Variant
  Return $key
End

Public Sub _new(count As Integer)
  For i As Integer = 0 To count - 1
     hendrix.push(i)
  Next
End

Private Function count_read() As Integer
  Return hendrix.count
End

Public Sub _next() As Variant
  If hendrix.count = 0 Then
      enum.stop
      Return
  Endif
  If IsNull(enum.index) Then
      enum.index = 0
      $key = 1
      Return hendrix[enum.index]
  Else
      Inc enum.index
      If enum.index > hendrix.max Then
         enum.stop
         Return
      Else
         $key = enum.index + 1
      Endif
  Endif
Return hendrix[enum.index]
End
End Class
----------------------------------------------------------------------------
Outputs
begin
Outer Value = 0 Key = 1
Outer Value = 1 Key = 2
Outer Value = 2 Key = 3
Outer Value = 3 Key = 4
   Inner Value = 0 Key = 1
   Inner Value = 1 Key = 2
   Inner Value = 2 Key = 3
   Inner Value = 3 Key = 4
   Inner Value = 4 Key = 5
   Inner Value = 5 Key = 6
   Outer Value = 3 Key = 6
Outer Value = 4 Key = 5
Outer Value = 5 Key = 6
End

--
~~~~ Brian

Attachment: OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


Follow-Ups:
Re: nested _next for same object class, confusionBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>