Structures |
Structs are programmer-defined data value types, which do not support inheritance, (they cannot be derived from). Allocating structs requires less overhead than creating instances of a class, so using structs instead of classes can improve performance.

The syntax for declaring a struct is similar to that of declaring a class:
struct StructName { //Member Declarations }
Constructors and Destructors
Structs can have instance and static constructors, but destructors are not allowed.
There is a predefined implicit parameter-less constructor (which cannot be deleted or redefined) supplied by the compiler for every struct. This constructor sets each of the struct’s members to the default value for that type (value members are set to their default values).
You can create additional constructors as long as they have parameters.
For example:
public struct MySimpleStruct { public int X, Y; // Constructor with parameters public MySimpleStruct(int a, int b) { // } class MyClass { // } // Call implicit constructor MySimpleStruct s1 = new MySimpleStruct(); // Call constructor MySimpleStruct s2 = new MySimpleStruct(5, 10); }
You can also create an instance of a struct without using the new operator; however, there are some restrictions:
the value of a data member cannot be used until it's explicitly set
a function member of the struct can be called only after all data members have been assigned.
Static Constructors
The static constructors of structs (just like classes) create and initialize the static data members and cannot reference instance members. Static constructors for structs follow the same rules as those for classes.
Type | Description |
Instance constructor (parameter-less) | Cannot be declared in the program. An implicit constructor is supplied by the system for all structs. It cannot be deleted or redefined by the program. |
Instance constructor (with parameters) | Can be declared in the program. |
Static constructor | Can be declared in the program. |
Destructor | Destructors are not allowed. Cannot be declared in the program. |
Field Initializers in structs
Field initializers are not allowed in struct declarations, as shown in the following code:
public struct MySimpleStruct { public int X = 5; // Not allowed! }
Passing Structs As Return Values and Parameters
SIMPL+ does not support structures as return values.
When declaring a SIMPL# structure variable within SIMPL#, all appropriate objects must be initialized.
Unfortunately there is no way for SIMPL+ to initialize structure objects, so the initialization must be implemented within the SIMPL# class. To accomplish this, a function can be written in SIMPL# which can initialize and return the allocated object:
SIMPL#:
namespace SIMPLSharpLibrary1 { // Base structure public struct baseLibStruct { public int i; } // Tag structure that includes baseLibStruct public struct tagLibStruct { public int j; public string structString; public baseLibStruct baseLibStruct; } // Base class public class baseClass { public void baseClassFn() { ErrorLog.Notice("BaseClassFn() Called."); } } // Derived class that includes tagLibStruct public class class1 : baseClass { public int x; public tagLibStruct libStruct; public int InitStruct(ref tagLibStruct myStructArg) { myStructArg.structString = string.Empty; myStructArg.structString = "abc"; myStructArg.j = 1; myStructArg.baseLibStruct.i = 2; return 0; } public string StringFunction(string input) { return "Processed: " + input; } } }
The following code is an example how to declare and call structures defined within a SIMPL# Library from a SIMPL+ Module:
#USER_SIMPLSHARP_LIBRARY "SIMPLSharpLibrary1" function Classes() { string str[255]; tagLibStruct libStruct; class1 myclass, myClassArr[10]; myClass.InitStruct(libStruct); myClass.x = 1; myClass.libStruct.j = 2; myClass.libStruct.baseLibStruct.i = 3; myClass.baseClassFn(); str = myClass.StringFunction( "Hello" ); print("Value of myClass.StringFunction = %s", str); myClassArr[1].x = 1; myClassArr[2].libStruct.j = 2; myClassArr[3].libStruct.baseLibStruct.i = 3; myClassArr[4].baseClassFn(); } function Structures() { tagLibStruct libStruct, libStructArr[10]; libStruct.j = 1; libStruct.baseLibStruct.i = 2; libStructArr[1].j = 1; libStructArr[2].baseLibStruct.i = 2; }

![]() |
Copyright (C) 2013 to the present, Crestron Electronics, Inc. All rights reserved. No part of this work may be reproduced in any form, machine or natural, without the express written consent of Crestron Electronics.