Can a Structure be passed into Define_Function
mush
Posts: 287
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
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
0
Comments
For example, let's say that in define_type you have created a data type called Dick:
and in define_variable you have created a variable of type Dick named Bill.
In your function definition you would say something like:
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:
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
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
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.
More important, how did ?mush? stick? It?s one of my favorite nicks on the forum.
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.
I'll PM you the story so as not to clog up the thread.
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.
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.