Named Enumerations

Just starting out? Need help? Post your questions and find answers here.
Fred
Administrator
Administrator
Posts: 18249
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Named Enumerations

Post 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).
User avatar
mk-soft
Always Here
Always Here
Posts: 6253
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Named Enumerations

Post 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

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 4221
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Named Enumerations

Post 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?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 6253
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Named Enumerations

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Fred
Administrator
Administrator
Posts: 18249
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Named Enumerations

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4221
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Named Enumerations

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply