Page 3 of 3

Re: SetProp() without RemoveProp()..?

Posted: Thu May 06, 2010 6:38 pm
by mesozorn
Thanks guys, the hash map solution seems like it will be a good one, so once I move to PB 4.50 I will almost certainly begin using it regularly. I'm working in 4.3 presently to complete a large-ish project begun with that version a while back, so wasn't aware of the built-in maps functionality in the newer version. I confess I'm not immediately sure how I'd implement the map-in-structure functionality under 4.3, at least in a way that would allow it to take randomly made-up on-the-fly property name strings the way the map can (as opposed to pre-concocted property names that could be included ahead of time as part of a sub-structure), but that's alright.. I'll give it some thought if I need it as you guys have already been more than helpful enough.

Much appreciated...

Re: SetProp() without RemoveProp()..?

Posted: Sat May 08, 2010 2:09 am
by mesozorn
Having finally had a chance to try out hash maps under PB 4.50, I thought I'd post this alternate native version of SetProp/GetProp functionality, which seems to work effectively as well.

Code: Select all

Global NewMap Properties.l()

Macro SetProp(gadget,prop,value)
  Properties(Str(gadget)+"_"+prop)=value
EndMacro

Macro GetProp(gadget,prop)
  Properties(Str(gadget)+"_"+prop)
EndMacro

Macro RemoveProp(gadget,prop)
  DeleteMapElement(Properties(),Str(gadget)+"_"+prop)
EndMacro

If OpenWindow(0, 0, 0, 270, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
  TextGadget(1, 10,  70, 100, 20, "TextGadget One", #PB_Text_Center)
  TextGadget(2, 140,  70, 100, 20, "TextGadget Two", #PB_Text_Center)
    
  SetProp(1,"Property1",11111)
  SetProp(2,"Property2",22222)
  Debug GetProp(1,"Property1")   
  Debug GetProp(2,"Property2")   
  RemoveProp(1,"Property1")
  RemoveProp(2,"Property2")
  Debug GetProp(1,"Property1")   
  Debug GetProp(2,"Property2")   
       
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
    
EndIf

Here it is combined with Freak's example implementation from the Tips & Tricks forum:

Code: Select all

Global NewMap Properties.l()

Macro SetProp(gadget,prop,value)
  Properties(Str(gadget)+"_"+prop)=value
EndMacro

Macro GetProp(gadget,prop)
  Properties(Str(gadget)+"_"+prop)
EndMacro

Macro RemoveProp(gadget,prop)
  DeleteMapElement(Properties(),Str(gadget)+"_"+prop)
EndMacro


; Example
;
Enumeration
  #Spin1
  #Spin2
  #Text1
  #Text2
EndEnumeration


If OpenWindow(0, 0, 0, 210, 100, "SpinGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  SpinGadget(#Spin1, 20, 20, 80, 25, 0, 100, #PB_Spin_Numeric)
  SpinGadget(#Spin2, 20, 55, 80, 25, 0, 100, #PB_Spin_Numeric)
  TextGadget(#Text1, 110, 20, 80, 25, "", #PB_Text_Border)
  TextGadget(#Text2, 110, 55, 80, 25, "", #PB_Text_Border)
 
  SetProp(#Spin1, "TextGadget", #Text1)
  SetProp(#Spin1, "Multiplier", 5)
  SetGadgetState(#Spin1, 0)
 
  SetProp(#Spin2, "TextGadget", #Text2)
  SetProp(#Spin2, "Multiplier", 10)
  SetGadgetState(#Spin2, 0)
 
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      Gadget = EventGadget()
      SetGadgetText(GetProp(Gadget, "TextGadget"), Str(GetGadgetState(Gadget) * GetProp(Gadget, "Multiplier")))     
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf