Home AMX User Forum NetLinx Studio

problem with modules

Hi!
I need your help.
I'm making a module:
MODULE_NAME='moduleName' (DEV TP, integer nMaxZone )

DEFINE_VARIABLES
integer zone[nMaxZone]

In the main Program I call the module by:
DEFINE_MODULE 'moduleName' moduleName(TP, NZone)

where NZone is defined :
DEFINE_VARIABLE
integer NZone=31


So:
- In the module, zone is defined as a integer variable, not array and the value is 1, but if I check the value of nMaxZone in DEFINE_START of the module, the value is 31 (correct)
Why?
Thanks a lot!!!
Alessandro

Comments

  • Spire_JeffSpire_Jeff Posts: 1,917
    Ok, declare the array in your main program and then pass it into the module.

    In Module:
    MODULE_NAME='moduleName' (DEV TP, integer zone[] )


    In Main Program:
    DEFINE_VARIABLES
    integer NZone[nMaxZone]

    DEFINE_MODULE 'moduleName' moduleName(TP, NZone)


    Jeff
  • DHawthorneDHawthorne Posts: 4,584
    You can't use a parameter passed to a module as a dimension for an array inside the module. The module variables determined compile time, not when it starts up, so it is always going to be wrong. Matter of fact, this won't work right in the main program either; you have to use constants for any array declaration. The only way around this is to declare it to the maximum size you think you will ever possibly need, and then use SET_LENGTH_ARRAY in startup to match your parameter.
  • a_riot42a_riot42 Posts: 1,624
    eddymouse wrote: »
    Hi!
    I need your help.
    I'm making a module:
    MODULE_NAME='moduleName' (DEV TP, integer nMaxZone )

    DEFINE_VARIABLES
    integer zone[nMaxZone]

    In the main Program I call the module by:
    DEFINE_MODULE 'moduleName' moduleName(TP, NZone)

    where NZone is defined :
    DEFINE_VARIABLE
    integer NZone=31


    So:
    - In the module, zone is defined as a integer variable, not array and the value is 1, but if I check the value of nMaxZone in DEFINE_START of the module, the value is 31 (correct)
    Why?
    Thanks a lot!!!
    Alessandro

    As Dave mentioned there are no dynamically allocated arrays in Netlinx, all array sizes must be determined at compile time. You might want to rethink your design of your module. Why pass it an integer when you can pass it an array that you have already defined in client code? Do what Jeff mentioned and that will work fine. I doubt your module knows better than the calling code about the number of zones, unless you are doing something unusual.

    I will mention though, that the styIe police will not like your use of the identifier nMaxZone. I think of Max as a maximum, like the most you can have and not to exceed, not just the count of the number of things you have.

    ie:
    iMaxTPChannels = 4000
    iMaxChars = 256

    If your variable really is a true maximum, then you can write the module like this:
    MODULE_NAME='moduleName' (dev TP, integer nNumZones[256] )

    Then in the module you can check to see if the length of the array passed is the same as the array in the above parameter and send a warning to the console on a timeline every 30 seconds if it isn't.

    I have written code where I have both a iNumSomething and a iMaxSomething variable and they mean very different things. Fortunately the style police are lenient and don't usually press charges for first offense :)
    Paul
Sign In or Register to comment.