bmmclure wrote:
Is there a way to specify a generic object with an arbitrary number of "fields" I can access? I guess it would be considered a generic 'struct'? Or would I be better off just creating a new class for an attribute?
That sounds like a class... or an Array (for a known number of fields). For example, if you had 5 fields, you could make a Class that contained those 5 fields. Or, you could create an Array of length 5 and store the values in it (via the
wrapper classes) and this would be your "struct".
bmmclure wrote:
In the Java code I'm referencing, to access the "uri" field of an attribute in the array, you'd use a call like:
Code:
data[index*5]
localName is:
Code:
data[index*5+1]
etc.
So data is an array of Strings, I take? The multiplier of 5 implies there are 5 such values in this "attribute". Then, each "field" in the "attribute" would be at a specified offset? It seems "uri" is at offset 0 and "localName" is at offset 1.
Also, since it is an array, the length is predetermined, right? Or is it unknown at creation?
Why not use an Array or Vector of Strings? Use the String class to wrap a String and create an Array or Vector of Strings. Then, do the same thing you would in java.
Code:
;"uri field"
;in Java (0-based index)
data[index*5]
;in AHK (1-based index)
Array_get(data, index*5+1)
Code:
;"localName field"
;in Java (0-based index)
data[index*5+1]
;in AHK (1-based index)
Array_get(data, index*5+2)
I'm not sure what needs you have, and a Class object might simplify them, but the above is the equivalent to the java code you presented. Just remember that when setting a value, you will need to wrap the string first.
Code:
;"localName field"
;in Java (0-based index)
data[index*5+1] = "Some name"
;in AHK (1-based index)
Array_set(data, index*5+2, String_new("Some name"))
The string will automatically be destroyed when the Array, data, is destroyed, so no worries there. You can just add 1 to the index (to convert from 0-based to 1-based index), so that's easy. The Array_get will automatically unwrap the String value, so this is done for you. You will, however, need to remember to wrap the String when setting the value.
I'm not sure if I answered your question. If not, could you explain what it is you are trying to do, and why the above suggestions don't work. This would help me answer your question better. Also, remember that a "struct" is nothing but a Class with no functions. In AHK, a "struct" would still be called a "class", but you can have it behave like a struct by defining no class functions (apart from the getters / setters, and the "required" functions (e.g. new, initClass, destroy)).
_________________
As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the
Class Library. Check out
my scripts.