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

Re: nested _next for same object class, confusion



On 5/30/24 13:46, Benoît Minisini wrote:
Le 30/05/2024 à 20:15, Brian G a écrit :
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.


'Enum.Index' is a Variant, so you can put whatever you want in it. In your case, you must put both the index and the key.

Another solution is making the 'Key' property compute the key from the current index, and not store it in a global variable.

Regards,

I Tried that

The enum.index access caused the error 'Script Error > Not an enumeration' when not accessing it inside the _next function
here is the updated script doing just that.

To be clear I am trying to print the key from the first for each after the inner one completes

-------------------------------------------------------------------------------------------------------------
#!/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 SetKey
  Public Key As Integer
  Public index As Integer
End Class

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 enum.index.key                     ' doing this outside the actual _next causes error
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
  Dim DynKey As SetKey
  If hendrix.count = 0 Then
      enum.stop
      Return
  Endif
  If IsNull(enum.index) Then
      DynKey = New SetKey
      enum.index = DynKey
      enum.index.key = 1
      enum.index.index = 0
      Return hendrix[enum.index.index]
  Else
      Inc enum.index.index
      If enum.index.index > hendrix.max Then
         enum.stop
         Return
      Else
         enum.index.key += 1
      Endif
  Endif
Return hendrix[enum.index.index]
End
End Class

----------------------------------------------------------------------------------------------
--
~~~~ Brian

Attachment: OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


References:
nested _next for same object class, confusionBrian G <brian@xxxxxxxxxxxxxxxx>
Re: nested _next for same object class, confusionBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>