Pages

Wednesday, March 2, 2011

Control Names

Every control on the panel is represented by a number that is unique to the panel it is on. We use this number every time we want to address the control: place a value in it, take a value from it, change it's attributes etc.

But we don't want to write lines such as

SetCtrlVal (panelHandle, 7, 8);

GetCtrlVal (panelHandle, 8, x);

Why? Because we always need to consult a control-number table in order to find the correct number, and whenever we look at the code we don't immediately see what control is specified and what the code-lines are doing (can you tell what the get function above is doing? is it a mistake that there is no '&' near the x?).

Another more important reason we don't want to use numbers is that they might change if we modify the panel (adding or removing controls), so the numeric we want to address who was once represented by the number 5, might be represented by the number 3 after editing the panel.

Luckily there is another way to address a control: Every control, in addition to the number that represents it, is given a constant name that is defined in the GUI's header file (this is done automatically, so don't worry about it), it look something like this:

...

#define PANEL_SPEED_NUMERIC 7

#define PANEL_NAME_STRING 8

...

These constant names can be written instead of the numbers, and they will be replaced before the code is compiled. So now we can rewrite the two 'set' and 'get' lines in this manner:

SetCtrlVal (panelHandle, PANEL_SPEED_NUMERIC, 8);

GetCtrlVal (panelHandle, PANEL_NAME_STRING, x);

and the meaning is made clear: placing a new speed value and acquiring a name string. This two lines are exactly the same as the two lines that used numbers instead of names, but I am sure you will agree that they are much better.

And even if the numbers will change and shift, the names will always be correct.

The constant names, as you probably know by now, are comprised of the panel's constant name and the personal name of the control, for example the default name of the initial panel is PANEL, and if we create a numeric control with the constant name NUMERIC it's full name, to use in the code, will be PANEL_NUMERIC (this is the name that is define in the header file).

If you modify the constant names of the controls or the panel (double click on them to access their attributes), please use capital letters only, that way such precompiler constants are easily identified as such.

Bottom line - always use constant names, it is easier for you and it is better programming that will allow you to avoid unnecessary bugs.