DataType

# Structure

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

A <span class="title-ref">Structure</span> is a compound value type. It consists of a series of fields that are grouped together as a single block. You can control the size and order of the fields. A <span class="title-ref">Structure</span> can provide a convenient alternative to the `MemoryBlock</api/language/memoryblock>`. You will typically only use structures when you have very specific memory or performance requirements or when you need to interface with an outside API that requires a <span class="title-ref">Structure</span>. Instead of structures, you will usually want to use a Class with the appropriate properties for most of your data management.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                   | Type                               | Read-Only | Shared |
|------------------------|------------------------------------|-----------|--------|
| `Size<structure.size>` | `Integer</api/data_types/integer>` | ✓         |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                 | Parameters                                         | Returns                                           | Shared |
|--------------------------------------|----------------------------------------------------|---------------------------------------------------|--------|
| `ByteValue<structure.bytevalue>`     | littleEndian As `Boolean</api/data_types/boolean>` | `UInt8()</api/data_types/additional_types/uint8>` |        |
| `StringValue<structure.stringvalue>` | littleEndian As `Boolean</api/data_types/boolean>` | `String</api/data_types/string>`                  |        |

## Property descriptions

<div id="structure.size">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Structure.Size

**Size** As `Integer</api/data_types/integer>`

> The total size of the <span class="title-ref">Structure</span> in bytes.
>
> This property is read-only.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This property is not currently supported for Android.
>
> </div>

## Method descriptions

<div id="structure.bytevalue">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Structure.ByteValue

**ByteValue**(littleEndian As `Boolean</api/data_types/boolean>`) As `UInt8()</api/data_types/additional_types/uint8>`

> Gets the <span class="title-ref">Structure</span> data using a byte array.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not currently supported for Android.
>
> </div>

<div id="structure.stringvalue">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Structure.StringValue

**StringValue**(littleEndian As `Boolean</api/data_types/boolean>`) As `String</api/data_types/string>`

> Gets the StringValue of the <span class="title-ref">Structure</span>.
>
> The StringValue getter and setter methods let you treat the <span class="title-ref">Structure</span> as a String. This is useful for copying structures into and out of `MemoryBlock</api/language/memoryblock>`, for reading and writing structures to files, and for transmitting structures through sockets.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not currently supported for Android.
>
> </div>

## Notes

Structures cannot be created in code. You must create them in the IDE by adding a <span class="title-ref">Structure</span> to your project item.

Once you have defined a <span class="title-ref">Structure</span>, you can use it in almost any context where you would use any other data type. You use the same dot syntax to access <span class="title-ref">Structure</span> fields as you would use to access object properties, but when you use dot syntax with a <span class="title-ref">Structure</span>, you are manipulating the <span class="title-ref">Structure</span> variable itself, not a reference to data somewhere else.

`ByRef</api/language/byref>` works the same with <span class="title-ref">Structure</span> as it does with other non-intrinsic data types: a `ByRef</api/language/byref>` <span class="title-ref">Structure</span> passes the pointer to the <span class="title-ref">Structure</span>. This can be useful with Declares.

<div class="important">

<div class="title">

Important

</div>

For Android, the following types are supported for use with \`Structures\`: `Integer</api/data_types/integer>`, `UInteger</api/data_types/additional_types/uinteger>`, `Double</api/data_types/double>`, `Single</api/data_types/single>`, `Int64</api/data_types/additional_types/int64>`, `UInt64</api/data_types/additional_types/uint64>`, `Int32</api/data_types/additional_types/int32>`, `UInt32</api/data_types/additional_types/uint32>`, `Int16</api/data_types/additional_types/int16>`, `UInt16</api/data_types/additional_types/uint16>`, `Int8</api/data_types/additional_types/int8>`, `UInt8</api/data_types/additional_types/uint8>`, `Byte</api/data_types/additional_types/byte>`, `Boolean</api/data_types/boolean>`, `CGFloat</api/data_types/additional_types/cgfloat>`, `Color</api/data_types/color>`, `String</api/data_types/string>`, `CString</api/data_types/additional_types/cstring>`, `PString</api/data_types/additional_types/pstring>`, `WString</api/data_types/additional_types/wstring>` and `Ptr</api/data_types/additional_types/ptr>`.

</div>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Arrays

<span class="title-ref">Structure</span> fields can be defined as arrays, using the usual array syntax:

``` xojo
fieldName(LastIndex) As DataType
```

Arrays in <span class="title-ref">Structure</span> fields can't be manipulated in the same ways as normal arrays; they represent a fixed chunk of storage inside the <span class="title-ref">Structure</span>, not a dynamic object that can be resized and manipulated. <span class="title-ref">Structure</span> field arrays cannot be resized, cannot be assigned, and do not support any of the array methods.

<div class="important">

<div class="title">

Important

</div>

Properties of type Array are not currently supported for Android Structures.

</div>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Strings

Strings also have a special syntax and behavior inside a \`Structure\`:

``` xojo
fieldName As String * size
```

When you create fields in a <span class="title-ref">Structure</span>, you indicate the number of bytes it will use. A string in a <span class="title-ref">Structure</span> is a simple array of bytes. Unlike String variables, a string field has a fixed size and does not store text encoding information. If a string value contains fewer bytes than the declared size, unassigned bytes are assigned null bytes. If you use the Len function to get the length of the field, it will return the length of the string.

Just as you convert text to a specific encoding when writing it to a file or a socket, and assign the correct encoding to it when reading it back in, you must convert strings to a specific encoding when you assign them to a <span class="title-ref">Structure</span> and define them to the correct encoding when reading them back out.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Using structures

Once you've defined a <span class="title-ref">Structure</span>, you can use it in almost any context where you would use any other data type. Use the dot syntax to access the fields. You can define an object or module property as a <span class="title-ref">Structure</span>; you can declare a method parameter as a <span class="title-ref">Structure</span>; you can even embed one <span class="title-ref">Structure</span> as a field in another.

In addition to the fields you define, structures contain three built-in items:

| Name        | Parameters                                         | Description                                                                                                                                                                                                                                                                                                               |
|-------------|----------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Size        |                                                    | This constant returns the total size of the <span class="title-ref">Structure</span> in bytes.                                                                                                                                                                                                                            |
| ByteValue   | littleEndian As Boolean                            | Gets or sets the <span class="title-ref">Structure</span> data using a byte array.                                                                                                                                                                                                                                        |
| StringValue | littleEndian as `Boolean</api/data_types/boolean>` | Gets the StringValue of the structure. You must pass the desired endianness, which should match the LittleEndian property on the `MemoryBlock</api/language/memoryblock>` on `BinaryStream</api/files/binarystream>`. StringValue will convert the structure's fields to or from the appropriate endianness as necessary. |

The StringValue getter and setter methods let you treat the <span class="title-ref">Structure</span> as a `String</api/data_types/string>`. This is useful for copying structures into and out of `MemoryBlocks</api/language/memoryblock>`, for reading and writing structures to files, and for transmitting structures through sockets.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### <span class="title-ref">Structure</span> alignment

Introduced 2008r5 <span class="title-ref">Structure</span> alignment refers to aligning the data at a memory offset equal to some multiple of the word size. Data alignment can increase the computer's performance.

Structures can be aligned using Attributes. You add the attribute *StructureAlignment* to a <span class="title-ref">Structure</span> and use one of the legal values: 0, 1, 2, 4, 8, 16, 32, 64, and 128 as the value. A value of 0 indicates that the compiler should perform a natural alignment, which ensures that the <span class="title-ref">Structure</span> will be laid out correctly for a given OS platform's ABI (app binary interface) rules.

To specify a <span class="title-ref">Structure</span> alignment, add an attribute to the Attribute List in the Inspector. Specify *StructureAlignment* in the Name field and enter the desired alignment value.

## Sample code

Suppose you have created a <span class="title-ref">Structure</span> called Employee that is defined as follows:

``` xojo
EmpNumber As Integer
EmpOffice As String * 50
EmpPhone As String * 20
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

To work with the <span class="title-ref">Structure</span>, you can declare a variable or property as a <span class="title-ref">Structure</span> and get and set the fields that you declared. For example,

``` xojo
Var person As Employee
person.EmpNumber = 5
person.EmpOffice = "Tyler Hall"
person.EmpPhone = "555-1212"
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Then you can get any of the values in the <span class="title-ref">Structure</span>, i.e.,

``` xojo
MessageBox(person.EmpOffice)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can also assign a <span class="title-ref">Structure</span> to a `Variant</api/data_types/variant>`. The `VarType</api/language/vartype>` of a <span class="title-ref">Structure</span> is 36. and get the value as a string or another <span class="title-ref">Structure</span>. For example:

``` xojo
Var v As Variant

v = person ' entire structure
v = person.EmpOffice ' one field
v = person.StringValue(False) ' Gets the entire structure as a string
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you create a public <span class="title-ref">Structure</span> in a class, then you would declare an instance outside the class with the syntax *ClassName.StructureName*, e.g.

``` xojo
Var person As MyClass.Employee
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Declare</api/language/declare>` command; `Variant</api/data_types/variant>` data type; `VarType</api/language/vartype>` function; `BinaryStream</api/files/binarystream>`, `MemoryBlock</api/language/memoryblock>` classes, `Advanced Language Features-Structures</getting_started/using_the_xojo_language/advanced_language_features/structures>` topic

</div>
