Show/Hide Toolbars

XSharp

Purpose

Declare a class name to the compiler.

Syntax

 [Attributes] [Modifiers] STRUCTURE <idStructure>
 [IMPLEMENTS <idInterface>[, <IdInterface2>,..]

 [StructureMembers]

 END STRUCTURE

Arguments

AttributesAn optional list of one or more attributes that describe meta information for am entity, such as for example the [TestMethod] attribute on a method/function containing tests in a MsTest class library. Please note that Attributes must be on the same line or suffixed with a semi colon when they are written on the line above that keyword.

 

ModifiersAn optional list of modifiers that specify the visibility or scope of the entity, such as PUBLIC, PROTECTED, HIDDEN, INTERNAL, SEALED, ABSTRACT or STATIC.

 

<idStructure>A valid identifier name for the class.  A class is an entity and, as such, shares the same name space as other entities.  This means that it is not possible to have a class and a global variable, for example, with the same name.

 

IMPLEMENTS <idInterface>The name(s) of the interface(s) that this class implements

 

StructrureMembersThis can be any of ACCESS, ASSIGN, CONSTRUCTOR, DESTRUCTOR, EVENT, METHOD, OPERATOR, PROPERTY, just like in the CLASS declaration

 

In this case, the variables x and z are typed as INT, while the variables cName and cAddr are typed as STRING.

Description

After the structure name is declared to the compiler, it is followed by 0 or more instance variable declaration statements.  You use a structure name to declare variables (see GLOBAL and LOCAL statements in this guide) designed to hold instances of a specific class, to instantiate instances of the class, and to define methods (see the METHOD statement in this guide) and subclasses for the class.

Notes

Binding of instance variables: Instance variables can be either early or late bound, depending on how you declare them and how you use them.

 

Early binding happens if the memory location of a variable is known at compile time.  The compiler knows exactly how to reference the variable and can, therefore, generate code to do so.

 

Late binding is necessary if the memory location of a variable is unknown at compile time.  The compiler cannot determine from the program source code exactly where the variable is or how to go about referencing it, so it generates code to look the symbol up in a table.  The lookup is performed at runtime.

 

Since there is no need for a runtime lookup with early bound instance variables, using them instead of late bound variables will significantly improve the performance of your application.  The following table summarizes the binding and visibility issues for the four types of instance variables:

 

Variable TypeBinding        Visibility        
EXPORTEarly, if possible        Application-wide for CLASS and module-wide for STATIC CLASS        
INSTANCEAlways late        In class and subclasses        
HIDDENAlways early        In class only        
PROTECTAlways early        In class and subclasses        

 

Object instantiation: Once you declare a class, you create instances of the class using the class name followed by the instantiation operators, {}.  The syntax is as follows:

 

<idClass>{[<uArgList>]}

 

where <uArgList> is an optional comma-separated list of values passed as arguments to a special method called Init() (see the METHOD statement in this guide for more information on the Init() method).

 

Accessing instance variables: The syntax to access an exported instance variable externally (i.e., from any entity that is not a method of its class) is as follows:

 

<idObject>:<idVar>

 

You can access non-exported instance variables only from methods in which they are visible.  Within a method, you use the following syntax for accessing all instance variables:

 

[SELF:]<idVar>

 

The SELF:  prefix is optional except in the case of an access/assign method (see the ACCESS and ASSIGN statement entries in this guide for more information and the METHOD statement for more information on SELF).

Instance variables are just like other program variables.  You can access them anywhere in the language where an expression is allowed.

 

The prefix [STATIC] is no longer supported  by XSharp

Examples

The following example defines two classes, one of which inherits values from the other, and demonstrates how to create a class instance with initial values for the instance variables:

 

 

See Also

ACCESS, ASSIGN, CONSTRUCTOR, DESTRUCTOR, EVENT, METHOD, OPERATOR, PROPERTY