ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / OOP (Object Oriented Programming)
  • 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
  • Immutability of 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
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Class overview
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.
    Universal Windows 10 App 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.).
Example of class
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()
  • FileTime()
  • FileSize()
This class has no property.
The following information is displayed in the "Project explorer" pane:
Project explorer,
See the Notes to get the description of the different icons used.
Creating and declaring a class

Creating a class

To create a class:
  1. Display the "Project explorer" pane if necessary: on the "Home" tab, in the "Environment" group, expand "Panes" and select "Project explorer".
  2. In the "Project explorer" pane, open the context menu of the "Classes" folder and select "Create a class".
  3. In the window that is displayed, enter the name of the class and validate.
  4. 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

<NomClasse> est une Classe [, abstraite]
<Portée et droits>
<Déclaration membre 1>
<Déclaration membre 2>
<Portée et droits>
<Déclaration membre 3>
<Déclaration membre 4>
...
<CONSTANTE>
<Déclaration des constantes>
FIN
Declaring a class

Syntax

<NomClasse> est une Classe [, abstraite] [, immuable]
  ...
FIN
<ClassName>
Name identifying the class.
abstract: optional
Used 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.
New in version 2024
immutable: optional
Indicates that all class members are immutable. In this case, the value of immutable members is defined either when the member is declared, or in the class constructor.

Example

ClasseSystème is Class
Nom is string
END
Declaring the members of a class

Syntax

[GLOBAL] [<Mode d'accès>]
<Nom du membre> est un <Type du membre> [ = <Valeur initiale>]
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>
Initial value of member.
ClasseSystème is Class
Nom is string
GLOBAL PRIVATE
TailleClasse is int
END

Special case: Declaring Array members

To declare an array in a class, use the following syntax:
ClasseSystème est une Classe
TableauDéfaut est un tableau de 1 entier
FIN
The array is declared in the same way as simple arrays (for more details, see Array type).
When copying instances of classes, all the members of the class are copied into the new instance except for the arrays. Therefore, if the value of an array member is modified, this value is modified in all the instances.
To get independent arrays in all instances of classes, a local array must be declared as follows:
ClasseSystème est une Classe
TableauDéfaut est un tableau local de 1 entier
FIN

Remark: To get independent arrays in all instances of classes:
  1. Open the project description window: on the "Project" tab, in the "Project" group, click "Description".
  2. In 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:
    <Nom du membre> est un <Type du membre> [ = <Valeur initiale>]
    [, Sérialise = <Vrai/Faux>]
  • Serialization and change of member name:
    <Nom du membre> est un <Type du membre> [ = <Valeur initiale>]
    [, Sérialise = <Nouveau nom>]
    Example:
Cl is Class
// Membre sérialisé
MembreSérialisé is int 
// Membre non sérialisé
MembreNonSérialisé is string, serialize = false  
// Membre renommé lors de la sérialisation
MembreRenommé is int, serialize = "NouveauNomMembre" 
END
New in version 2024

Immutability of class members

A class member can be immutable. In this case, once its value has been set, it cannot be changed.. The value of immutable members is defined either when the member is declared, or in the class constructor.
To define the immutability of a member, it is possible to:
  • specify immutability at class level. In this case, all class members will be immutable..
  • specify immutability at limb level.
The syntax for defining member immutability is as follows:
<Nom du membre> est un <Type du membre> [ = <Valeur initiale>] [, immuable]
Example:
Cl is Class, immutable
Membre1 is int = 5
id is int // id sera défini dans le constructeur
END
Cl is Class
Membre1 is int, immutable = 5
id is int, immutable // id sera défini dans le constructeur
END

Data binding

The Data binding is available for the members of the class.
Declaring the class constants

Syntax

CONSTANT <Nom de la constante> = <Valeur de la constante>

CONSTANT
<Nom de la constante> = <Valeur de la constante>
<Nom de la constante> = <Valeur de la constante>
FIN

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:
<Nom classe singleton> est une Classe
GLOBAL
<Nom instance> est <Nom classe singleton>
FIN


Example
CSingleton is Class
GLOBAL
m_Singleton is CSingleton
END
Creating and declaring the class methods

Creating class methods

To create a class method:
  1. Display the "Project explorer" pane if necessary: on the "Home" tab, in the "Environment" group, expand "Panes" and select "Project explorer"..
  2. In the "Project explorer" pane, display the available classes: to do so, expand the "Classes" folder.
  3. Select the desired class. Right-click to open the context menu of the class and select "New method".
  4. 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.
  5. The code editor displays the code of new method.

Syntax for declaring a method

PROCEDURE [<Accès>] [Globale] [VIRTUELLE] [ABSTRAITE]
<Nom de la méthode> ([<Paramètre1>, ...[<ParamètreN>]]) [<Attributs d'extension>]

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: Optional
Defines 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.
PROCÉDURE GLOBAL VoirObjet(obj)
// Le membre privé TailleClasse est accessible depuis le code de la classe
Info("Nom: " + obj:Nom + "Taille: " + obj:TailleClasse)
FUNCTION GLOBAL Essai(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.
Universal Windows 10 App The class properties are not available.

Creating a class property

To create a class property:
  1. Display the "Project explorer" pane if necessary: on the "Home" tab, in the "Environment" group, expand "Panes" and select "Project explorer".
  2. In the "Project explorer" pane, display the available classes (expand the "Classes" folder).
  3. Select the desired class. Display the popup menu of the class and select "New property".
  4. In the window that is opened, enter the name of the property and validate.
  5. 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.
Universal Windows 10 AppAndroidAndroid Widget Java This feature is not available.

Syntax

The following syntax must be used:
// Déclaration de la classe
ClasseHFSQLIndépendant est une Classe, contexte HFSQL indépendant [<léger/complet>]
FIN
ou
// Déclaration de la classe
ClasseHFSQLIndépendant est une Classe
<contexte HFSQL indépendant léger/complet>

FIN
For example:
ClasseHFSQLIndépendant 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.
Notes

Legend of icons used

The different icons used are as follows:
Public procedure
Public procedure
Protected procedure
Protected procedure
Private procedure
Private procedure
Public virtual method
Public virtual method
Protected virtual method
Protected virtual method
Private virtual method
Private virtual method
Public global method
Public global method
Protected global method
Protected global method
Global private method
Global private method
Constant
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:
WD Simple OOP 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,
- ...
Minimum version required
  • Version 10
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 09/27/2023

Send a report | Local help