Page 3 of 3

Re: Named Enumerations

Posted: Thu Apr 12, 2018 8:31 pm
by Fred
Yes it's reason to use an array, to have a fast access with a number. But there is a chance of conflit if you use too high indexed numbers, as #PB_Any returns the memory pointer of the list item (if you use an index number of 20 million and the memory allocator returns a pointer with 20 million, you can't tell who is who).

Re: Named Enumerations

Posted: Thu Apr 12, 2018 8:51 pm
by mk-soft
I like index arrays for object (Gadgets, etc)

Code: Select all

;-TOP

Enumeration Windows
  #Main
EndEnumeration

; Enumeration Gadgets
;   #Gadget0
;   #Gadget1
;   #Gadget2
;   #Gadget3
;   #Gadget4
;   #Gadget5
;   #Gadget6
;   #Gadget7
; EndEnumeration

; -------------------------------------------------------------------

Prototype MyProtoInvoke()

Structure udtCallEvent
  Invoke.MyProtoInvoke[0]
EndStructure

Global *CallEventGadget.udtCallEvent = ?CallEventGadgetVT

#CallEventGadgetCount = 8

Procedure EventGadgetCB()
  Protected Gadget = EventGadget()
  If Gadget >= 0 And Gadget < #CallEventGadgetCount
    *CallEventGadget\Invoke[Gadget]()
  EndIf
EndProcedure

BindEvent(#PB_Event_Gadget, @EventGadgetCB())

; -------------------------------------------------------------------

Procedure Main()
  Protected x, y
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 530, 70, "Purebasic", #PB_Window_SystemMenu)
    For x = 0 To 3
      For y = 0 To 1
        gadget = y * 4 + x
        ButtonGadget(gadget, x * 130 + 10, y * 30 + 10, 120, 25, "Gadget " + Str(gadget))
      Next
    Next
    ; Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
      EndSelect
    ForEver
  EndIf
EndProcedure : Main()

; -------------------------------------------------------------------

Procedure Gadget0()
  Debug "ButtonGadget 0"  
EndProcedure

; -------------------------------------------------------------------

Procedure Gadget1()
  Debug "ButtonGadget 1"  
EndProcedure

; -------------------------------------------------------------------

Procedure Gadget2()
  Debug "ButtonGadget 2"  
EndProcedure

; -------------------------------------------------------------------

Procedure Gadget3()
  Debug "ButtonGadget 3"  
EndProcedure

; -------------------------------------------------------------------

Procedure Gadget4()
  Debug "ButtonGadget 4"  
EndProcedure

; -------------------------------------------------------------------

Procedure Gadget5()
  Debug "ButtonGadget 5"  
EndProcedure

; -------------------------------------------------------------------

Procedure Gadget6()
  Debug "ButtonGadget 6"  
EndProcedure

; -------------------------------------------------------------------

Procedure Gadget7()
  Debug "ButtonGadget 7"  
EndProcedure

; -------------------------------------------------------------------

DataSection
  CallEventGadgetVT:
  Data.i @Gadget0()
  Data.i @Gadget1()
  Data.i @Gadget2()
  Data.i @Gadget3()
  Data.i @Gadget4()
  Data.i @Gadget5()
  Data.i @Gadget6()
  Data.i @Gadget7()
EndDataSection


Re: Named Enumerations

Posted: Thu Apr 12, 2018 9:05 pm
by skywalk
Not sure about speed difference when cycling small list() vs small array().
Would anyone notice gui changes using #PB_Any or #Enum's?

Re: Named Enumerations

Posted: Thu Apr 12, 2018 10:34 pm
by mk-soft
I don't think anyone will notice a difference.

It's a question of the programming technique you want to use.
For static different windows I would recommend enumeration, and for the same windows that are opened multiple times, dynamic id (PB_Any)

Re: Named Enumerations

Posted: Fri Apr 13, 2018 3:32 pm
by Fred
If you mean replacing the array by a list, it will be some noticeable difference for a GUI with many controls you want to resize for example. As it is now, there is no speed difference between #Number and #PB_Any.

Re: Named Enumerations

Posted: Fri Apr 13, 2018 3:54 pm
by skywalk
Hi Fred,
I'm asking why there are 2 gadget data structures behind the scenes?
Is it not possible to use an array for #PB_Any gadgets?
Like after compilation, translate the list() to array()?
Then, both coding styles behave the same at runtime.