web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / DaxGeek / Collection Classes "St...

Collection Classes "Struct"

Hossein.K Profile Picture Hossein.K 6,648
Struct objects (short for structures) are entities that can hold a number of values
of any X++ type.
Structs are used to group information about a specific entity.
For example, there may be a need to store information about a customer's name,
group and address and then treat this compound information as one item.
The following methods are commonly used on
struct objects:

• add(str _fieldName, anytype _value) - adds a new field to the
struct and assigns the specified value to it.


• exists(str _fieldName) - determines whether a particular field exists
in a struct.


• fieldName(int _index) - returns the name of the field in the struct at
the position specified.

 
• fields() - returns the number of fields in the struct.

• index(str _fieldName) - calculates the position of a field in the
struct based on its name.


• remove(str _fieldName) - removes a field from a struct.

• value(str _fieldName, anytype _value) - gets or sets the value for a
specific field.


• valueIndex(int _index, anytype _value) - gets or sets the value of
the field at a specific index position in a struct.


The fields in a struct are specified by a data type and a name. Fields can be
added when the
struct is created by using the new() method, or they can be
created after the struct has been created using the
add() method. If a field is
added using the
add() method, the value is specified for the field at the same
time, and the data type is inferred from this value. If a field is added during the
instantiation of the struct, the
value() or the valueIndex() method needs to be
used to assign a value to it.
The fields in a struct are arranged in alphabetical order of the field names.
The following is an example using the Struct object.

   

 1
2
3
4
5
6
7
8
9
10
11
Struct struct = new Struct();
int i;
// Add new fields and values
struct.add("Group", "DOM");
struct.add("Name", "Jane Doe");
struct.add("Shoesize", 45);
// Prints the type and name of all items in the struct
for (i = 1 ; i <= struct.fields() ; i++)
{
info(strfmt("%1 : %2",truct.fieldType(i),struct.fieldName(i)));
}

The result in the infolog should be:

• String : Group

• String : Name

• Integer : Shoesize

Best Regards,
Hossein Karimi  

Comments

*This post is locked for comments