- Class overview
- Example of class
- Creating and declaring a class
- Creating a class
- Syntax for declaring the class and its elements
- Declaring a class
- Syntax
- Example
- Declaring the members of a class
- Syntax
- Special case: Declaring Array members
- Serializing the members of a class
- Defining immutable class members
- Data binding
- Declaring the class constants
- Syntax
- Detailed syntax
- Example
- Creating and declaring a singleton class
- Creating and declaring the class methods
- Creating class methods
- Syntax for declaring a method
- Detailed syntax
- Deleting a method
- Creating and declaring properties
- Overview
- Creating a class property
- Data binding
- Access rights to a property
- Members and properties
- Inheritance
- Passing properties as parameters to a WLanguage function or procedure
- Class instances with independent HFSQL context
- Overview
- Syntax
- Use
- Notes
- Legend of icons used
- New features since version 9
Classes, members, methods and properties
A class is made of: - data, called members.
- constants.
- procedures, called methods. A method is a procedure specifically written to handle the objects found in the class.
- properties. A property is a class element that can be directly used by its name as a member and for which the operations for value assignment and retrieval trigger the execution of a process.
The class properties are not available.
To use a class, you must declare one or more objects. All the objects of a given class have the same attributes and behavior, but their members contain different data. By default, the members of a class are public. They can be accessed by a method of the class as well as by a process/event of the project or its elements (control, window, etc.). The "CFile" class includes the members: m_sShortPath, m_sLongPath, m_sExtension, m_sShortName, m_sLongName. The "CFile" class includes the following methods: | | - FileDate()
- FileSelection()
| |
This class has no property. The following information is displayed in the "Project explorer" pane:
See the Notes to get the description of the different icons used. Creating and declaring a class Creating a class To create a class: - Display the "Project explorer" pane if necessary: on the "Home" tab, in the "Environment" group, expand "Panes" and select "Project explorer".
- In the "Project explorer" pane, open the context menu of the "Classes" folder and select "Create a class".
- In the window that is displayed, enter the name of the class and validate.
- The code editor displays the code of new class. The "Declaration" event of the class as well as the constructor and the destructor are automatically created.
Syntax for declaring the class and its elements <ClassName> is Class [, abstract] <Scope and rights> <Declaration member 1> <Declaration member 2> <Scope and rights> <Declaration member 3> <Declaration member 4> ... <CONSTANT> <Declaration of constants> END Syntax <ClassName> is Class [, abstract] [, immutable] ... END <ClassName> Name identifying the class. abstract: optionalUsed to specify that the class is abstract. An abstract class is a base class that groups behaviors common to several classes. A class is abstract when it is declared as such or if one of its methods is abstract. An abstract class cannot be instantiated. immutable: optionalIndicates that all class members are immutable. In this case, the value of the immutable members is set when the member is declared or in the class constructor. Example SystemClass is Class
Name is string
END
Declaring the members of a class Syntax [GLOBAL] [<Access mode>] <Member name> is <Member type> [ = <Initial value>] GLOBAL: Optional Defines a global member. This member will exist outside the objects. It can be used without instantiating an object. If several objects are declared, a global member is common to all objects. <Access mode>: Optional Used to restrict the access to this class member.- Public (by default): Access allowed from any code of the class or project.
- Public CONSTANT: The value of the member can be read and modified from a code of the class or from a code of a derived class; it can only be read from another code of the application.
- Protected: Access allowed from a code of the class or from a code of a derived class (and "inherited" class)
- Protected CONSTANT: The value of the member can be read and modified from a code of the class; it can only be read from a code of a derived class; it cannot be accessed from any other code of the application.
- Private: Access allowed from a code of the class.
<Member name> Name identifying the member. <Member type> Type of the member chosen among the available types of WLanguage. <Initial value> SystemClass is Class
Name is string
GLOBAL PRIVATE
ClassSize is int
END
Special case: Declaring Array members To declare an array in a class, use the following syntax: SystemClass is Class DefaultArray is array of 1 int END The array is declared in the same way as simple arrays (for more details, see Array type). When copying class instances, all the members of the class are copied to the new instance, except for the arrays. Therefore, if the value of an array member is modified, it is also modified in all the instances. To get independent arrays in all instances of classes, a local array must be declared as follows: SystemClass is Class DefaultArray is local array of 1 int END
Remark: To get independent arrays in all instances of classes: - Open the project description window: on the "Project" tab, in the "Project" group, click "Description".
- On the "Compilation" tab, check "Arrays: the assignment copies the content".
For more details, see Project description, compilation tab.Serializing the members of a class By default, all the members of a class are serialized. You have the ability to precisely manage the serialization of each class member: - by specifying the member that will be serialized during the call to Serialize.
This operation can be performed on all types of serialization (WXML, JSON, binary). - by changing the name of the member during the serialization with Serialize.
This operation can be performed during a binary serialization only.
This management of serialization is performed by using the following syntax: - Serialization (or not) of a member:
<Member name> is <Member type> [ = <Initial value>] [, Serialize = <True/False>] - Serialization and change of member name:
<Member name> is <Member type> [ = <Initial value>] [, Serialize = <New Name>] Example:
Cl is Class
SerializedMember is int
NonSerializedMember is string, serialize = false
RenamedMember is int, serialize = "NewMemberName"
END
Defining immutable class members A class member can be immutable. This means that once its value has been set, it cannot be changed. The value of an immutable member is set in its declaration or in the class constructor. You can define an immutable member: - at class level. In this case, all class members will be immutable.
- at member level.
Use the following syntax to define an immutable member: <Member name> is <Member type> [, immutable] [ = <Initial value>] Example: Cl is Class, immutable
Member1 is int = 5
id is int
END
Cl is Class
Member1 is int, immutable = 5
id is int, immutable
END
Data binding The Data binding is available for the members of the class. Declaring the class constants Syntax CONSTANT <Constant name> = <Constant value>
CONSTANT <Constant name> = <Constant value> <Constant name> = <Constant value> END Detailed syntax <Constant name>
Name defined for the constant. A constant is public. <Constant value>
Value associated with the constant. This value will not change during the program execution. Example CONSTANT K=5
CONSTANT
K=5
J=10
END
Creating and declaring a singleton class A Singleton is a declaration of unique class instance. To declare a unique class instance and to allocate it immediately, use the following syntax: <Name singleton class> is Class GLOBAL <Instance name> is <Name singleton class> END Example CSingleton is Class
GLOBAL
m_Singleton is CSingleton
END
Creating and declaring the class methods Creating class methods To create a class method: - Display the "Project explorer" pane if necessary: on the "Home" tab, in the "Environment" group, expand "Panes" and select "Project explorer"..
- In the "Project explorer" pane, display the available classes: to do so, expand the "Classes" folder.
- Select the desired class. Right-click to open the context menu of the class and select "New method".
- In the window that appears:
- Type the name of method.
- Specify whether the comment of the method prototype will be automatically generated ("Generate a header comment" option).
For more details on automatically generated comments, see Automatic documentation of procedures and methods. - Specify whether the method is public, protected or private. For more details, see the "Access" section of the Detailed syntax.
- Specify whether the method is abstract. For more details, see the "Abstract" section of the Detailed syntax.
- Specify whether the method is global. For more details, see the "Global" section of the Detailed syntax.
- Validate.
- The code editor displays the code of new method.
Syntax for declaring a method PROCEDURE [<Access>] [Global] [VIRTUAL] [ABSTRACT] <Method name> ([<Parameter1>, ...[<ParameterN>]]) [<Extension attributes>] Detailed syntax <Access>: Optional Used to restrict the access to this method. 3 levels are available: - Private: the method can only be called from a code of the class
- Protected: the method can only be called from a code of the class or from a code of a derived class
- Public (by default): the method can be called from any code of the class or project.
Global: Optional Defines a global method. This method will not operate on a specific object: no class object is required to call this method. This class can also be used to handle the global members. Virtual: Optional Defines a virtual method. An overridden method is virtual by default. Abstract: OptionalDefines an abstract method. An abstract method is a method that must absolutely be redefined in the derived classes. A method is not abstract by default. <Method name> Name identifying the method. <Parameter 1> ... <Parameter N> Optional parameters to pass to the method. <Extension attribute>Extension attributes used to define the options for managing the code editor. The available extension attributes are: - <zombie [comment = "texte"]>: Used to define an obsolete method (also called zombie method). The optional comment keyword is used to specify the text that will be displayed in the compilation error associated with the obsolete method. For more details, see Zombie procedures.
- <without step by step>: Used to specify that this method will be ignored by the debugger when debugging in "Step by step" mode.
GLOBAL procedure ViewObject(obj)
Info("Name: " + Obj:Name + "Size: " + Obj:ClassSize)
GLOBAL FUNCTION Attempt(a1,b1)
RETURN a1 + b1
Deleting a method A method can be deleted: - from the "Project explorer" pane ("Delete" in the context menu)
- from the code editor, via the context menu of the method bar ("Delete").
Creating and declaring properties Overview A property is a code element with two events: - an event for retrieving the value,
- an event for assigning the value.
A property can be used like a variable or like a member (direct retrieval of the value, assignment via the '=' symbol, ...). At runtime: - Any operation that requires reading the property runs the event for retrieving the value. This event must return a value.
- Any operation that requires writing the property runs the event for assigning the value. This event must process a parameter.
Creating a class property To create a class property: - Display the "Project explorer" pane if necessary: on the "Home" tab, in the "Environment" group, expand "Panes" and select "Project explorer".
- In the "Project explorer" pane, display the available classes (expand the "Classes" folder).
- Select the desired class. Display the popup menu of the class and select "New property".
- In the window that is opened, enter the name of the property and validate.
- The code editor shows the events related to the property:
- Event for retrieving the property. This event contains:
- the "RESULT" keyword that is used to get the value of the property.
- the "RETURN =" keyword to return the value of the property.
- Event for assigning the property. This event is used to give a value to the property. This value is passed as parameter. This event must not return a result.
Remark: A new property can also be created in the context menu of a member. In this case, the procedures automatically handle the specified member. Data binding The Data binding is available for the properties of classes and call members. Access rights to a property The property cannot be read if the retrieval event is empty. A compilation error will appear in the editor and an error will occur at runtime. The property cannot be written if the assignment event is empty. A compilation error will appear in the editor and an error will occur at runtime. The retrieval and assignment events can be public, private or protected. The access rights of the property correspond to the less restrictive rights of the two events. For more details, see Access rights to a property. The properties can be global. A property is global to the class when the retrieval and assignment events are global. If one of the events is global, all events must be global, otherwise a compilation error is displayed. Members and properties A property and an existing member can have the same name. In this case, the priority orders are as follows: - in the events of the property, the member has priority over the property.
- in the rest of the code, the property has priority over the member.
Therefore, a member can be replaced by a property without modifying the code that uses the member. The event of a property cannot recursively use the property. If no member has the same name as the property, using the property in one of its events will cause a compilation error. Inheritance The properties of base classes are inherited in the derived classes. A property can be overloaded in a derived class. The events of a property are always considered as being virtual: the use of the VIRTUAL keyword is ignored. The multiple inheritance is supported by the properties. The following syntaxes can be used to call the properties of the base classes: - Ancestor.Property if the derived class has a single base class
- BaseClass.Property otherwise.
Passing properties as parameters to a WLanguage function or procedure If the parameter is passed by reference (default case) and untyped: - the property is passed as parameter.
- no events associated with the property are run during the call.
- the reading of the formal parameter runs the retrieval code of value.
- the writing of the formal parameter runs the assignment code of value.
If the parameter is by passed value (LOCAL) and untyped: - the property value is passed as parameter.
- the code for retrieving the value is run during the call.
- the reading of the formal parameter accesses the value passed during the call and it does not run the retrieval code of value.
- the writing of the formal parameter modifies the value passed during the call and it does not run the assignment code of value.
If the parameter is passed by reference (default case) and typed: - the property is passed as parameter.
- the type of the property must be correct. Otherwise, a WLanguage error occurs.
- no events associated with the property are run during the call.
- the reading of the formal parameter runs the retrieval code of value.
- the writing of the formal parameter runs the assignment code of value.
If the parameter is by passed value (LOCAL) and with specific type: - the property value is passed as parameter.
- the code for retrieving the value is run during the call.
- if necessary, the value of the property is converted into the type of the parameter.
- the reading of the formal parameter accesses the value passed during the call and it does not run the retrieval code of value.
- the writing of the formal parameter modifies the value passed during the call and it does not run the assignment code of value.
Class instances with independent HFSQL context Overview A copy of the HFSQL context can be associated with a class instance. All the accesses to HFSQL performed from the methods and properties of the class (calls to functions or access to the records) operate on the copy of the context of the class instance. Syntax The following syntax must be used: // Declare the class IndependentHFSQLClass is Class, independent <light/full> HFSQL context END or // Declare the class IndependentHFSQLClass is Class <independent light/full HFSQL context>
END For example: IndependentHFSQLClass is Class, independent light HFSQL context
Light/Full parameter: - The "light" keyword triggers the immediate copy of part of HFSQL context.
Only the directories containing the data files in HFSQL Classic mode and/or the connections in HFSQL Client/Server mode are stored. - The "full" keyword triggers the immediate copy of HFSQL context.
Recommended if the thread must take into account the current positions in the files and queries of caller context.
Use The HFSQL context of instance is created by copying the current context during the constructor execution. The = and <= operators between two instances with independent HFSQL context copy the content of the HFSQL context. It is not recommended to use class instances with independent HFSQL contexts to simulate an alias on a single HFSQL file or on a specific record of an HFSQL file. Indeed, copying the HFSQL context affects all context elements (files, views, queries, connections, ...) and it can take quite a long time. A class instance with independent HFSQL context can only be used in the thread that run the constructor. To handle a class instance with independent HFSQL context in several threads, you must: - allocate a new instance of the class,
- perform a copy of the source instance with the = or <= operator.
Legend of icons used The different icons used are as follows: | | | Public procedure | | Protected procedure | | Private procedure | | Public virtual method | | Protected virtual method | | Private virtual method | | Public global method | | Protected global method | | Global private method | | Constant |
New features since version 9 A class containing a character member does not have the same behavior as in the earlier versions. Indeed, the characters were coded on 1 byte in the earlier versions and on 2 bytes since version 9. We recommend that you use the "1-byte integer" type.
Related Examples:
|
Training (WINDEV): WD Simple OOP
[ + ] The "WD Simple OOP" example is an educational example about the OOP with WINDEV. This example presents the operating mode of: - classes, - inheritances, - virtual procedures, - UML diagrams, - ...
|
This page is also available for…
|
|
|