Constant in DEFINE_CONSTANT with and w/o datatypes
microchip78
Posts: 25
Hi Experts,
I was reviewing AMX code developed by someone else and I come across following piece of code ...
DEFINE_CONSTANT
// VC Screen Modes
VC_Makeacall = 1;
VC_CallProgress = 2;
VC_RecentCalls = 3;
VC_Videoscreen = 4;
// Day and Date
Integer DaySunday = 1;
Integer DayMonday = 2;
Integer DayTuesday = 3;
Integer DayWednesday = 4;
Integer DayThursday = 5;
Integer DayFriday = 6;
Integer DaySaturdday = 7;
Above code is compile piece of code. Can anyone please tell me what is the difference constant without any datatypes and one with datatypes?
Thanks in Advanced.
MC78
I was reviewing AMX code developed by someone else and I come across following piece of code ...
DEFINE_CONSTANT
// VC Screen Modes
VC_Makeacall = 1;
VC_CallProgress = 2;
VC_RecentCalls = 3;
VC_Videoscreen = 4;
// Day and Date
Integer DaySunday = 1;
Integer DayMonday = 2;
Integer DayTuesday = 3;
Integer DayWednesday = 4;
Integer DayThursday = 5;
Integer DayFriday = 6;
Integer DaySaturdday = 7;
Above code is compile piece of code. Can anyone please tell me what is the difference constant without any datatypes and one with datatypes?
Thanks in Advanced.
MC78
0
Comments
But afaik constants are direct substitutions at compile time, so there is no need to type them, you aren't giving the compiler any directives, it is just substituting in what ever you have made them equal to, so there should not be any ambiguity at all in their types as, as I said, they are just subsitituted in line with the rest of the code.
If anyone has a reason to type their constants can you let me know as I've always been a little confused as to why people do.
Help me out here!
So if you want to use anything other than single integers or char arrays as constants then the type has to be declared. I suppose I could just type the constants that don?t fit the defaults but I personally feel it?s better to type them all. Perhaps my wife is correct and I am indeed strange.
My primary use of DEFINE_CONSTANT is to "name" array indexes so I know what the heck I was doing when I look at it two months later. For example: Now, whenever I need to work with dvHousePanels, I can refer to it as dvHousePanels[KITCHEN_PANEL] and know right away which one I am calling, where dvHousePanels[1] may not be so obvious. I'll do something similar with multi-room audio zones, inputs, just about anything I may need a lookup table for. It also makes it a lot easier to change later ... if your zones are in an array, and the configuration changes, you only have to change the values in DEFINE_CONSTANT, not go through every line of code to make sure you didn't miss any.
Constants are definitely typed, they aren't macro substitutions like a #define in C. You can't use them to substitute arbitrary strings or expressions in your program. Just like variables, though, the INTEGER part is assumed if you don't mention it (unless you put in a string/array like Joe said). Since most people use only integer constants, though, it frequently gets omitted (harmlessly).
- Chip
Dare Aint nufink wrong wif my spelink.
I have the following code that seems to contradict your statement about using constants as string expressions.
This code works just as I expected it to. When I view the string GarageDoors[1].NAME it's value is North Door.
Macro substitution (aka global search and replace, a la C's #define pragma) would let you do useful things like
And thats why North Door works the way it does. You can declare a constant as 'YOUR MOM' with no problem, but you can't declare one as {1,3,3,7} without declaring it as an integer
Although not a real macro substitution for C style macros, using the #DEFINE compiler directive the following code is actually working:
However, it is not possible to use dynamic substitutions using variables as it would be possible in C.
Patrick