Anyone have some sample code that uses structures? I didn’t find any in the Xojo Examples folder. I’m able to successfully create a Structure, and add fields in the attributes editor. I do not have any success in accessing those structure fields by name however.
Dim struct As MyStructureType struct.FieldName = 123
Thank you. I’m hoping for a small full project example rather than two simple lines of code.
What is the mystery here? It should be dead simple. Perhaps you could show us some code you have tried and we can help you amend it.
The only glitch I can think of is you have to have a variable or property of your structure type and operate on that. You can’t address the name of your structure directly. See Andrew’s first line of code. That’s really all there is to it.
The Structure page in the docs does show an example structure with sample code on how to access it.
With that said, unless you have some very specific need (interfacing with OS APIs, for example), I’d recommend using a Class instead of a Structure.
Completely agree with this.
Ten years ago when I was still learning Xojo, I started using structures to organize some data.
Classes are much better for this, especially that they can be subclassed and methods can be overloaded.
I appreciate the feedback and advice.
I believe I do have a very specific need such as interfacing with OS APIs. I’m well aware of the benefit of classes over structures, however would like to successfully understand proper usage of the latter in Xojo to determine their applicability to my needs.
The structure page does indeed give a good example, however it lacks complete details. Specifically I am uncertain of how to define the fields of the structure itself in the attributes section of the structure in the IDE. It requires name and value pairs. I presume the names are the structure field names, what are the values? Types? Byte Sizes? Default values? I’m missing something very basic.
Having a complete simple Xojo program would likely answer these and possibly other questions. My attempts so far, cause Xojo to crash on compile (not execution) [I’ve submitted it to tech.] So I’m clearly not fully clued in yet.
Considering when you create a new entry, the default Text is
Fieldname as Type
I’d think that would be pretty obvious.
The only one that’s probably not obvious is how to deal with string buffers since you need to specify a certain length l, in which case you’d use:
S as String * 5
Over on the right, you’ll see a column indicating the size of each item and the offset from the beginning of the structure.
The last line shows the total length of the struct.
Thanks Greg. That was the missing piece. I was amusingly trying to edit the attributes instead of seeing there was a “+” for definitions.
Code and understanding working well now.