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: Declaration of 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
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
Class overview
A class is made of:
  • 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.
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 is made up of the following 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. If necessary, display the "Project Explorer" pane: under the "Home" pane, in the "Environment" group, pull down "Panes" then 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

<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
Declaring a class

Syntax

<ClassName> is Class [, abstract] [, immutable]
  ...
END
<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.
immutable: optional
Indicates 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 (default): Access authorized from any code in the class or project.
  • Public CONSTANT: the member's value can be read and modified from code in the class or in a derived class, and only read from other code in the application..
  • Protected: Access authorized from class code or code from a derived (and "inherited") class.
  • Protected CONSTANT: Member value can be read and modified from within class code, only read from derived class code, and inaccessible from other application code..
  • Privé: Access authorized by a class code.
<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.
SystemClass is Class
	Name is string
	GLOBAL PRIVATE
		ClassSize is int
END

Special case: Declaration of 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

Note: To obtain independent arrays in all class instances:
  1. Open the project description window. To do so, go to the "Project" tab, "Project" group, and click "Description".
  2. In the "Compilation" tab, check the "Arrays: assignment copies contents" option..
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
	// Serialized member
	SerializedMember is int 
	// Non-serialized member
	NonSerializedMember is string, serialize = false  
	// Member renamed during the serialization
	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 // id will be defined in the constructor
END
Cl is Class
	Member1 is int, immutable = 5
	id is int, immutable // id will be defined in the constructor
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:
  1. If necessary, display the "Project Explorer" pane: under the "Home" pane, in the "Environment" group, scroll down to "Panes" and select "Project Explorer"...
  2. In the "Project Explorer" pane, display the various classes available: simply pull down 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 [<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:
  • Privé: the method can only be called from code in the
  • Protected: method can only be called from code within the class or from code in a derived class
  • Public (default): the method can be called from any code in the class or project.
Globale: 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 = "text"]>: Allows 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.
  • <sans pas à pas>: Indicates that this method will be ignored by the debugger when debugging in "step-by-step" mode..
GLOBAL procedure ViewObject(obj)
// The private member ClassSize is accessible from the class code
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:
  1. If necessary, display the "Project Explorer" pane: under the "Home" pane, in the "Environment" group, pull down "Panes" then select "Project Explorer".
  2. In the "Project explorer" pane, display the available classes (expand the "Classes" folder).
  3. Select the desired class. Open the context menu of the class and select "New property".
  4. In the window that opens, 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 from a member's context menu. 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 with 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. Property events are always considered virtual: the use of the VIRTUAL keyword is unnecessary and 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 not typed:
  • 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 passed by value (LOCAL) and not typed:
  • 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 passed by value (LOCAL) and typed:
  • 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.
AndroidAndroid Widget Java This feature is not available.

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.
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: 10/04/2024

Send a report | Local help