Show/Hide Toolbars

XSharp

Purpose

Declare a class name to the compiler.

Syntax

 [Attributes] [Modifiers] CLASS <idClass> [INHERIT <idClass>]
 [IMPLEMENTS <idInterface>[, <IdInterface2>,..]

 [ClassMembers]

 END CLASS

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.

 

 

<idClass>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.

 

INHERIT <idClass>The name of an existing class (called a superclass) from which the new class inherits methods and instance variables (with the exception of HIDDEN).

 

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

 

ClassMembersThis can be any of Instance Variables, ACCESS, ASSIGN, CONSTRUCTOR, DESTRUCTOR, EVENT, METHOD, OPERATOR, PROPERTY

Description

After the class name is declared to the compiler, it is followed by 0 or more instance variable declaration statements.  You use a class 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:

 

FUNCTION Start()
 LOCAL oCust AS Customer
 oCust := Customer{"Louis", 92.07.22, "GA", 987}
 oCust:DisplayAll()
 ? oCust:Name
 ...
 
// Declare Person class
 
CLASS Person
 EXPORT Name AS STRING
 INSTANCE Birth AS DATE
 
// Declare Customer class to inherit from Person
 
CLASS Customer INHERIT Person
 PROTECT CustNum AS SHORTINT
 INSTANCE Address AS STRING
 
// Declare method to initialize instance variables
// Note that cName and dBirth are available to the
// Customer class, even though they are not declared
// as part of the class — they are inherited from Person
 
METHOD Init(cOne, dTwo, cThree, nFour) ;
         CLASS Customer
 Name := cOne
 Birth := dTwo
 Address := cThree
 CustNum := nFour
 RETURN SELF
 
// Declare method to display all instance variables
 
METHOD DisplayAll() CLASS Customer
 ? "Name:     ", Name
 ? "Birth Date:  ", Birth
 ? "Address:   ", Address
 ? "Number:    ", CustNum

See Also

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