Page 1 of 2

"Me" keyword for current gadget number

Posted: Fri Aug 14, 2009 2:01 pm
by PB
I would like to see an internal variable (called "Me" like in Visual Basic)
that holds the current gadget number that was created by any of the
gadget commands, to reduce typing the same gadget name over and
over. Sort of like With/EndWith. Here's what I mean:

Code: Select all

Enumeration
  #NumberSelection
  #NameList
EndEnumeration

If OpenWindow(0,300,250,400,200,"test",#PB_Window_SystemMenu)

  ComboBoxGadget(#NumberSelection,10,10,70,22)

    AddGadgetItem(#NumberSelection,-1,"One")
    AddGadgetItem(#NumberSelection,-1,"Two")
    AddGadgetItem(#NumberSelection,-1,"Three")
    SetGadgetText(#NumberSelection,"One")
    GadgetToolTip(#NumberSelection,"Sets the number")

  ListIconGadget(#NameList,100,10,200,100,"Names",180)

    AddGadgetItem(#NameList,-1,"Fred")
    AddGadgetItem(#NameList,-1,"Freak")
    AddGadgetItem(#NameList,-1,"Berikco")
    DisableGadget(#NameList,#True)
    GadgetToolTip(#NameList,"List of team members")

  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

EndIf
The above code could be replaced with something like this:

Code: Select all

Enumeration
  #NumberSelection
  #NameList
EndEnumeration

If OpenWindow(0,300,250,400,200,"test",#PB_Window_SystemMenu)

  ComboBoxGadget(#NumberSelection,10,10,70,22)

    AddGadgetItem(Me,-1,"One")
    AddGadgetItem(Me,-1,"Two")
    AddGadgetItem(Me,-1,"Three")
    SetGadgetText(Me,"One")
    GadgetToolTip(Me,"Sets the number")

  ListIconGadget(#NameList,100,10,200,100,"Names",180)

    AddGadgetItem(Me,-1,"Fred")
    AddGadgetItem(Me,-1,"Freak")
    AddGadgetItem(Me,-1,"Berikco")
    DisableGadget(Me,#True)
    GadgetToolTip(Me,"List of team members")

  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

EndIf
See how it eliminates the need to type the gadget's constant name over
and over? The internal variable "Me" would just be updated by the
compiler every time a new gadget command is reached. Don't give it
a long name (like "CurrentGadget") because that defeats the purpose.
I just want something quick to type in place of the gadget number so
I don't need to type the name again and again every time I add to it.

Posted: Fri Aug 14, 2009 2:17 pm
by magicjo
Fred tell me that the feature will be added for 4.50 version :lol: , for now use the macros.

Posted: Fri Aug 14, 2009 2:21 pm
by freak
Can't you just use a variable called "Me" ?

Posted: Fri Aug 14, 2009 2:25 pm
by blueznl
Instead of 'Me' one could use '#TheNrOfTheLastGadgetIReferencedToInMyCode', but I have to admit that isn't shorter ;-)

I see the point though :-)

Posted: Fri Aug 14, 2009 2:33 pm
by milan1612
Seems reasonable to me and could be quite useful. But I'd like it to work with all
PB identifiers, with files or network connections and so on...

Posted: Fri Aug 14, 2009 2:36 pm
by blueznl
A keyword like '#Last'... but that could get complicated when mixing up types...

Then #LastGadget, #LastFile etc. but those mean you'd have longer to type...

Unless the compiler would be smart enough and turn #Last into a contextual value based upon the preceeding source code. Hmm. Even a preprocessor could do that :-)

Posted: Fri Aug 14, 2009 2:52 pm
by PB
> Can't you just use a variable called "Me" ?

Okay, yes, but I didn't mention it could be used for more than just gadget
creation. For example, later in the same example app above, the compiler
could use "Me" again with a code block like this:

Code: Select all

ClearGadgetItems(#NameList)
AddGadgetItem(Me,-1,"gnozal")
AddGadgetItem(Me,-1,"Sparkie")
SetActiveGadget(Me)
To use a variable manually, means putting "Me=#NameList" everywhere in
my source, as well as for every other gadget. The aim is to save typing. ;)

Posted: Fri Aug 14, 2009 4:05 pm
by freak
To me this just sounds messy. It decreases the readability of the code imho. And wait until you copy/paste/move some code around and wonder why everything is messed up just because there is a gadget command somewhere in between.

Also you don't always need the compiler's help for things like this:

Code: Select all

Global Me

Procedure SetMe(Value)
  Me = Value
  ProcedureReturn Value
EndProcedure

ClearGadgetItems(SetMe(#NameList))
AddGadgetItem(Me,-1,"gnozal")
AddGadgetItem(Me,-1,"Sparkie")
SetActiveGadget(Me) 

Besides, your constant names are rather short. My longer ones look like this: :D

Code: Select all

#DEBUGGER_GADGET_Variable_InputRange

Posted: Fri Aug 14, 2009 4:12 pm
by Demivec
If the "Me" existed would it only be translated when used in those contexts or would you now lose the use of a variable with the same name?

I think that's way the names for things like this have always resembled something like #PBCompiler_LastGadgetNumber. It gave it meaning and also didn't interfere with your own names for things.


The idea seems useful but brings with it complications like the aforementioned.

Here's a crazy variation of it (that has its own problems):

Code: Select all

Enumeration
  #NumberSelection
  #NameList
EndEnumeration

If OpenWindow(0,300,250,400,200,"test",#PB_Window_SystemMenu)
  
  ComboBoxGadget(#NumberSelection,10,10,70,22)
  
  With #NumberSelection
    AddGadgetItem(##,-1,"One")
    AddGadgetItem(##,-1,"Two")
    AddGadgetItem(##,-1,"Three")
    SetGadgetText(##,"One")
    GadgetToolTip(##,"Sets the number")
  EndWith
  
  ListIconGadget(#NameList,100,10,200,100,"Names",180)
  
  With #NameList
    AddGadgetItem(##,-1,"Fred")
    AddGadgetItem(##,-1,"Freak")
    AddGadgetItem(##,-1,"Berikco")
    DisableGadget(##,#True)
    GadgetToolTip(##,"List of team members")
  EndWith
  
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
  
EndIf

Re: "Me" keyword for current gadget number

Posted: Fri Aug 14, 2009 8:00 pm
by cas
PB wrote:I just want something quick to type in place of the gadget number so
I don't need to type the name again and again every time I add to it.

Code: Select all

;{ 

Macro ComboBoxGadgetEx(gadget,x,y,Width,Height,Flags=0)
  ComboBoxGadget(gadget,x,y,Width,Height,Flags) : Me=gadget
EndMacro

Macro ListIconGadgetEx(gadget,x,y,Width,Height,Title,TitleWidth,Flags=0)
  ListIconGadget(gadget,x,y,Width,Height,Title,TitleWidth,Flags) : Me=gadget
EndMacro

;insert here all other macros...

Global Me ;this var can be made local if writing threaded app

;}

Enumeration
  #NumberSelection
  #NameList
EndEnumeration

If OpenWindow(0,300,250,400,200,"test",#PB_Window_SystemMenu)

  ComboBoxGadgetEx(#NumberSelection,10,10,70,22)

    AddGadgetItem(Me,-1,"One")
    AddGadgetItem(Me,-1,"Two")
    AddGadgetItem(Me,-1,"Three")
    SetGadgetText(Me,"One")
    GadgetToolTip(Me,"Sets the number")

  ListIconGadgetEx(#NameList,100,10,200,100,"Names",180)

    AddGadgetItem(Me,-1,"Fred")
    AddGadgetItem(Me,-1,"Freak")
    AddGadgetItem(Me,-1,"Berikco")
    DisableGadget(Me,#True)
    GadgetToolTip(Me,"List of team members")

  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

EndIf 
8)

Posted: Fri Aug 14, 2009 9:49 pm
by PB
> To me this just sounds messy

Visual Basic coders have done it that way for decades, as evidenced by lots
of source code listings on dozens of Visual Basic sites. It's tried and true. ;)

@Cas: Nice example, but doesn't fulfill the second part of my request, that
is, later calls to the gadget that was created. Also it means I have to add lots
of macros to my app (one for every gadget), which is time-wasting. (Example:
One of my apps has an on-screen keyboard, so I'd need 101 macros. :( ).

I think I'll try my hand at a pre-processor one day. I've never done it before.

Posted: Fri Aug 14, 2009 10:14 pm
by freak
Whats wrong with my example ? Its only seven characters more to type than your original proposal. You can't be that lazy :P

Posted: Fri Aug 14, 2009 10:23 pm
by cas
freak wrote:Whats wrong with my example ?
Your solution is definitely best here (and easiest to implement).

Posted: Fri Aug 14, 2009 10:24 pm
by Fred
You are misunderstanding the 'Me' keyword in VisualBasic. Visual Basic is an object oriented basic, and the 'Me' refers to the current object, like 'This' in JAVA or C#. It's only available when you act from inside the object. PB doesn't have object, so such a keyword is not possible.

Posted: Fri Aug 14, 2009 11:45 pm
by Kale
freak wrote:To me this just sounds messy. It decreases the readability of the code imho.
Yep, agreed! Just use a variable.
PB wrote:Visual Basic coders have done it that way for decades, as evidenced by lots of source code listings on dozens of Visual Basic sites.
Doesn't make it right or well designed.