Home AMX User Forum NetLinx Studio

Constant Declaration

I am confused by how the Netlinx compiler interprets the following code for constant declaration:
DEFINE_CONSTANT
CONSTANT CHAR DELIMITER = $0D;
CONSTANT CHAR ACCEPT[]	= "'OK',DELIMITER";
I would expect that const DELIMITER would evaluate to a char with value = 13 (or $0D in ascii).
I would also expect that const ACCEPT would evaluate to a string of length 3 with the value = OK,$0D.

This is not the case. Through the debug window it appears that ACCEPT has length 14 with value = 'OK',DELIMITER (NOTE: both the single quotes and comma are part of the value of ACCEPT).

Does anyone have some insight as to why the compilier interprets this code in this way?

Comments

  • The rules for declaring string constants are kinda whacky in Netlinx. Your best bet is to check out Tech Note 531 on AMX's website, which goes into it in great detail.

    Basically, you can't use double quotes in constant delcarations, so that's probably messing up your efforts the most.

    - Chip
  • Thanks Chip!

    That tech note does explain quite a bit on the subject. I guess I shouldn't just assume that the standards of Netlinx code carry over from one section of the code to the next.

    For future reference, here's the updated code that behaves properly:
    DEFINE_CONSTANT
    CONSTANT CHAR DELIMITER = $0D;
    CONSTANT CHAR ACCEPT[]	= {'O','K',DELIMITER};
    
  • DHawthorneDHawthorne Posts: 4,584
    Just as an aside, the CONSTANT CHAR is redundant in the DEFINE_CONSTANT section. You don't need it there, only if you were declaring them in DEFINE_VARIABLE (not sure why you would).
  • DHawthorne wrote:
    Just as an aside, the CONSTANT CHAR is redundant in the DEFINE_CONSTANT section. You don't need it there, only if you were declaring them in DEFINE_VARIABLE (not sure why you would).
    You're right, it wouldn't make any sense to declare constants in the variable section, but it is supported for some reason. I put CONSTANT and CHAR in front of my declarations out of habit from prior programming experiences, guess I just can't break myself of it. :)
  • dchristodchristo Posts: 177
    For what it's worth, I occasionally put constants in DEFINE_VARIBLE, next to other variables that reference them. Sometimes it's nice to have everything together to help keep it all in context.

    --D
  • Good idea
    dchristo wrote:
    For what it's worth, I occasionally put constants in DEFINE_VARIBLE, next to other variables that reference them. Sometimes it's nice to have everything together to help keep it all in context.

    --D

    I do the same, especially when declaring multiple Define_Variable sections that can be managed with code folding.
Sign In or Register to comment.