Home AMX User Forum NetLinx Studio

Can a Structure be passed into Define_Function

Does anyone know how or indeed if a structure can be passed into a Function?
AMX does it with the reserved word 'VARIANTARRAY' but sadly when I tried to use it I was told..
"C10248: Variant types are only for system use "
Any ideas?

Thanks in advance

Mush

Comments

  • HedbergHedberg Posts: 671
    yes, you can pass a structure to a function. Just include the structure in the parameter list like any other parameter.

    For example, let's say that in define_type you have created a data type called Dick:
    define_type
    
    structure Dick
    {
      integer IntegerVar1
    }
    
    and in define_variable you have created a variable of type Dick named Bill.
    define_variable
    
    Dick Bill
    

    In your function definition you would say something like:
    define_function integer Tom(Dick Harry)
    {
    //do something on the variable of type Dick called Harry
    return Harry.IntegerVar1
    }
    
    Note that the variable Harry of type Dick exists only in the function Tom

    So, in an event you could call the function Tom and send it the current values for Bill:
    for example:
    {
      Bill.IntegerVar1 = 1
      nVariable = Tom(Bill)
    }
    
  • Hi,

    One very common reason people write functions/calls in Netlinx is for buffer parsing, i.e. processing and making sense of the strings/data which has been sent back to the NXI from some device. As you probably know, it is often the case that such functions are defined/declared to accept "DEV" type parameters. Also, you probably know that there are many system calls which take "DEV" type parameters.

    Well it turns out that the "DEV" data type is not intrinsic to the Netlinx language - it is actually a structure (search for "STRUCTURE DEV" in the Netlinx Studio help system), which is defined as follows:

    STRUCTURE DEV
    {
    INTEGER Number // device number
    INTEGER Port // port on device
    INTEGER System // system device belongs to
    }


    So I would bet that you have already written calls or functions which take DEV data-types (i.e. structures)!

    By the way, we are all used to defining devices according to the "D:P:S" syntax in Netlinx. It turns out that this "D:P:S" syntax of referencing the structure members is peculiar to DEV types - i.e. you still have to reference your own structure members using the dot (".") notation. Also, if you have defined a device (e.g. dvDVD = 5001:1:0), then you can reference its members dvDVD.number, dvDVD.port, and dvDVD.system.

    Hope this helps.

    Happy New Year!

    Tom Fuke
  • mushmush Posts: 287
    Hedberg wrote: »
    yes, you can pass a structure to a function. Just include the structure in the parameter list like any other parameter.

    G'day Harold,

    Thanks for your time and input.
    What I am trying to do is write a global function where the user can pass in his own structure for processing which is of unknown size, elements and element types.
    Similar to the way one can pass any structure into XML_TO_VARIABLE.
    With your suggestion the structure is pre-determined and fixed in its element type.
    Sorry that I did not make my question concise enough.

    Cheers

    Mush

    PS. Harold, I thought it was funny that your function 'Tom' responded after you! :-P
  • jweatherjweather Posts: 320
    There is no (documented) support for variant types. Assuming you could pass one into a function, NetLinx doesn't support the introspection needed to determine what type, elements, etc., the parameter contained.
  • mushmush Posts: 287
    jweather wrote: »
    There is no (documented) support for variant types. Assuming you could pass one into a function, NetLinx doesn't support the introspection needed to determine what type, elements, etc., the parameter contained.

    Would introspection be required?
    The onus to ensure correct usage would fall upon the programmer, would it not?

    NetLinx supports introspection with 'varriantarray', the type descriptor for 'xml_to_variable'. A function to which a structure may be passed.
  • Joe HebertJoe Hebert Posts: 2,159
    mush wrote:
    What I am trying to do is write a global function where the user can pass in his own structure for processing which is of unknown size, elements and element types.
    Even if we could pass it, how would you process the unknown struct? We don?t have the necessary tools. Maybe I?m missing your point.

    More important, how did ?mush? stick? It?s one of my favorite nicks on the forum.
  • mushmush Posts: 287
    Joe Hebert wrote: »
    Even if we could pass it, how would you process the unknown struct? We don?t have the necessary tools. Maybe I?m missing your point.

    I'm writing a transform for the AMX internal functions XML_TO_VARIABLE and VARIABLE_TO_XML.
    If you have used these you will realise two things;
    1. They can turn a structure into correctly formed XML and more importantly they can dump that XML directly into a structure. No extra coding required. I like this.
    2. The XML generated has no formatting at all and includes a lot of unneeded data. I don't like this.
    My goal was to write a function that you can pass a structure and a file name into and have the function 'pretty up' the XML. I've had to do it another, less streamlined way.
    Joe Hebert wrote: »
    More important, how did ?mush? stick? It?s one of my favorite nicks on the forum.
    I'll PM you the story so as not to clog up the thread.
  • HedbergHedberg Posts: 671
    And I'm stuck trying to get the projector to turn on.
  • jweatherjweather Posts: 320
    mush wrote: »
    Would introspection be required?

    Sure. Let's imagine NetLinx lets you define a VARIANTARRAY parameter for your function. Okay, now what are you going to do with it? The only valid thing I can think of to do with it is to pass it to VARIABLE_TO_XML or _STRING.

    To actually do anything useful with it, you would need ways to find out what fields are in the structure definition, what data types they are, and let you access their contents. These features are commonly referred to as "type reflection" or "introspection" in languages like C# and Java.
  • DHawthorneDHawthorne Posts: 4,584
    jweather wrote: »
    There is no (documented) support for variant types. Assuming you could pass one into a function, NetLinx doesn't support the introspection needed to determine what type, elements, etc., the parameter contained.

    This is only a problem with modules. If the structure is defined in the same program as your function, both already have access to the metadata.
Sign In or Register to comment.