User Tools

Site Tools


struct_compound_data_type_.htm
Navigation:  Clarion.Net (Clarion#) > Clarion# Language Extensions > Complex Data Types >====== STRUCT (compound data type) ====== Previous pageReturn to chapter overviewNext page

NewCNet.jpg

label STRUCT [,IMPLEMENTS] ,STATIC] [,THREAD] [, TYPE]
    Related variables and methods
END

blk2blue.jpg

STRUCT Define a simple class type value structure
IMPLEMENTS Specify an INTERFACE for the STRUCT. This adds additional methods to the implementation of the STRUCT
STATIC Specify the related variables and methods' memory is permanently allocated.
THREAD Specify memory for all STRUCT variables are allocated once for each execution thread. Also causes Constructors and Destructors to be called on thread start and exit.
TYPE Specify the STRUCT is only a type definition and not also an object instance of the STRUCT.

STRUCT is a simple user-defined structure value type, and sometimes can be an alternative to a Class. STRUCTs contain data and the methods for acting on that data.

STRUCT is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory. The example below shows a simple STRUCT declaration.

A value type is a data type that is allocated on the stack, as opposed to a reference type which is allocated on the heap.

In the Clarion .NET STRUCT keyword is used as alternative to CLASS keyword to declare .Net value-types (analogous to struct in C#). The STRUCT can be used with some restrictions:

' Only .Net value-types and references are allowed to be used as member of Clarion .Net's STRUCT. It means, it's impossible to use CLASTRING (but, only reference to CLASTRING) and arrays (but, only references to arrays) as members of STRUCT.

' You cannot declare value-types members with parameters (i.e. DECIMAL(10,2))

' It's impossible to declare default constructor(without parameters) for STRUCT

' STRUCTs cannot inherit from classes or other STRUCTs

STRUCTs are value types ' when an object is created from a STRUCT and assigned to a variable, the variable contains the entire value of the STRUCT. When a variable containing a STRUCT is copied, all of the data is copied, and any modification to the new copy does not change the data for the old copy. Because STRUCTs do not use references, they do not have identity ' there is no way to distinguish between two instances of a value type with the same data.

Thus, STRUCTs have the following properties:

' STRUCTs are value types while CLASSes are reference types.

' When passing a STRUCT to a method, it is passed by value instead of as a reference.

' Unlike CLASSes, STRUCTs can be instantiated without using a new operator.

' STRUCTs can declare constructors, but they must take parameters.

' A STRUCT cannot inherit from another STRUCT or CLASS, and it cannot be the base of a CLASS. All STRUCTs inherit directly from System.ValueType, which inherits from System.Object.

' A STRUCT can implement interfaces.

The semantics of attributes in the STRUCT declaration is the same as in CLASS declaration.

Example:

bb   STRUCT

Aa    LONG          ! OK

Ssref &CLASTRING    ! OK

Ss    CLASTRING(10) ! Illegal

Arrrf LONG[]        ! OK (Reference to array)

Arr   LONG,DIM(10)  ! Illegal

Dc    DECIMAL(10,2) ! Illegal

Dcdf  DECIMAL       ! OK

    END

struct_compound_data_type_.htm.txt · Last modified: 2021/04/15 15:57 by 127.0.0.1