New WINDEV 2024 feature!
Help / WINDEV Tutorial / Tutorial - WLanguage basics
  • Lesson 8 - OOP
  • Concepts
  • OOP concepts
  • A simple example
  • Creating the class
  • Creating properties
  • Creating methods
  • Declaring and manipulating objects
  • Using the constructor
  • To sum up

Tutorial - WLanguage basics

Lesson 8 - OOP
We will cover the following topics:
  • Concepts of Object-Oriented Programming.
  • Examples of object declaration.
Durée de la leçon 30 min
Object-Oriented Programming (OOP) is designed for better code reusability. Programs developed in OOP are structured: they are divided into modules, and each module manages a functionality. These modules can easily be reused in other software. They group a set of procedures (called methods) and encapsulate the data structure on which the methods will act.
To use object-oriented programming, you must declare classes, members and associated methods.
First, let's take a look at the main concepts of OOP.

OOP concepts

A class contains the description of a data structure (members) and the procedures (methods) that handle the members.
Therefore, a class defines a type of data and its behavior.
A class is used to create objects. Each created object owns the members described in its class and can be manipulated via the methods of its class. An object is an instance of a class.
When a class is declared, an object must be associated with the class so it can be manipulated by all the methods of this class.
A member is a piece of data (or parameter) of the object.
A method is used to act on the object, to modify its members for example.
A method is a procedure. It works like a standard WLanguage procedure.
Inheritance makes it possible to include the characteristics of an existing class (base class) into a new class (derived class). Inheritance allows you to create a new data type from a known type in order to add features or to modify its behavior. Therefore, the base class will not be modified. A class can inherit from a class and become a subclass.
Objects from a derived class can access all methods, members and properties of ancestor classes. It is just as if these methods, members and properties were part of the derived class.
Constructor and Destructor
Constructor and Destructor are two important concepts since they make it possible to automatically call initialization methods when the object that represents the class is created (instantiated) or destroyed (deleted). These methods are automatically created when the class is created. By default, the code of these methods is empty.
  • The Constructor method of the class is automatically called when an object of the same type as this class is declared (instantiated).
  • The Destructor method of the class is automatically called when an object of the same type as this class is destroyed (deleted). The execution of the Destructor method depends on the life span of the object according to how it was declared (global or local),
Data encapsulation
Data encapsulation ensures that the data belonging to the object is not accidentally modified by functions (methods) external to the object.
This allows you to prevent the user of an object from accessing some or all of its members. The members whose access is not allowed are called private members.
These private members can only be accessed from the methods designed for this purpose in the class.
A simple example

Creating the class

We are going to create a class that represents a product and contains members and methods. This class will be instantiated and used by a procedure.
To complete the exercises in this lesson, we will work on the "WLanguage" project that we used in the previous lesson.
If necessary, open the "WLanguage" project you created in the first lesson of this tutorial (see A project to discover WLanguage in lesson 1).
First we will create the CProduct class:
  1. On the "Home" tab, in the "General" group, click "New".
  2. In the window that appears, click "Code", then "Class".
  3. Specify the name of the class (CProduct in our example).
  4. Validate. The WLanguage code of the class is displayed. It consists of three sections:
    • Declaration
    • Constructor
    • Destructor
  5. Enter the following code in the "Declaration" event:
    CProduct is Class

    mg_StockWarningThreshold is int = 20

    m_Reference is string
    m_Description is string
    m_Stock is int

    // Stock status
    StockStatus is Enumération
    This declaration code contains:
    • The class declaration. This class contains:
      • a public member that corresponds to the stock warning threshold. This member is public because this information is the same for all products.
      • three private members: the product reference, its description and its quantity in stock.
    • The declaration of an enumeration. This enumeration makes it possible to manage the stock status. This enumeration must be declared in the class declaration code so that it can used by one of the methods of the class.
  6. Save the class (Ctrl + S).

Creating properties

To use private members from outside the class, they must be associated to properties (Getter and Setter). We will create these properties.
To create the properties associated with the "m_Reference" member:
  1. In the "Project explorer" pane (on the left side of the environment):
    • Expand "Classes" and then expand "CProduct". The different elements of the class appear in the "Project explorer" pane.
    • Expand "Members".
  2. Select the "m_Reference" member.
  3. Right-click to open the context menu and select "Generate property". The window that appears allows you to define all the characteristics of the property:
    • The name of the property.
    • The scope of the property. There are multiple options that can be combined. The property can be:
      • Public, protected, restricted, protected restricted or private.
      • Abstract or global.
    • The property access mode (read and/or write).
    • The role of the property: Undefined, Business or UI.
  4. Write "Reference" for the name of the property and validate.
  5. Read and write properties are automatically created with the corresponding WLanguage code.
Remark: For more details on how to create and declare class methods, see Creating class methods.
Repeat these steps to generate:
  • the Description property associated with the m_Description member,
  • the Stock property associated with the m_Stock member.

Creating methods

We will now create the methods associated with the class. In our example, we will create 2 methods:
  • A method for adding products to the current stock: AddStock.
  • A method to know the status of the stock: StatusOfStock.
To create the AddStock method:
  1. In the "Project explorer" pane, select the CProduct class.
  2. Right-click to open the context menu and select "New method". The window that appears allows you to define all the characteristics of the method. Name the method "AddStock" and validate.
    The syntax is the same for class methods and procedures.
  3. The code editor shows the method. Type the following code:
    PROCÉDURE AddStock(LOCAL AdditionalStock is int): boolean

    // The stock passed as parameter must be valid
    IF AdditionalStock < 0 THEN
    // Cannot add negative stock
    RESULT False

    // Increase current stock with additional stock
    m_Stock += AdditionalStock

    // Added successfully
    RETURN True
    Let's take a look at this WLanguage code:
    • The AddStock method expects an integer as parameter, an returns a boolean.
    • The stock value is evaluated. If the stock is negative, the method returns False: the stock is not updated.
    • If the value is positive, the stock is incremented.
You can repeat these actions to create the StatusOfStock method. The code of this method is as follows:
PROCÉDURE StatusOfStock(): StockStatus 

// If the stock is empty
IF m_Stock = 0 THEN

// If stock quantity is below warning threshold
IF m_Stock <= mg_StockWarningThreshold THEN

// Stock is OK
This code uses the StockStatus enumeration declared in the class declaration code.
Our class is complete. Now, we will use it.

Declaring and manipulating objects

Objects are declared at the same time as the other variables. For our example, we will declare an object in the project initialization code.
To open the project initialization code:
  1. Right-click the "P" button next to the open element tabs. The context menu appears.
  2. Select "Element code".
  3. The code editor displays the different events associated with the project.
  4. Clear the initialization process and write the following code:
    AProduct is CProduct

    AProduct.Reference = "REF-123"
    AProduct.Description = "My product"

    This code declares and instantiates an object. Values are assigned to properties via the following syntax:
    <Object name>.<Property name> = Value
  5. Add the following lines to this code:
    • Call to the AddStock method:
    • Reading properties:
      Trace(AProduct.Reference + " " + AProduct.Description + " " + AProduct.Stock)
    • Enumeration:
      IF AProduct.StatusOfStock() = CProduct.STOCK_OK THEN
      Trace("Enough units in stock")
      Trace("Not enough units in stock")
  6. Let's test this code: click Test project in the quick access buttons.
  7. If necessary, open the "Debugger trace" pane to see the result: on the "Home" tab, in the "Environment" group, expand "Panes", select "Panes", and then select "Debugger trace".
  8. The "Debugger trace" pane shows the following data:
    REF-123 My product 500
    Enough units in stock
    We see the product details and the stock status.

Using the constructor

To finish this quick overview of OOP with WINDEV, we will modify our code to use the constructor. This constructor will allow us to initialize the warning threshold.

Open the class code: double-click CProduct in the "Project explorer" pane, for example.
Modify the class declaration by replacing the following line of code:
mg_StockWarningThreshold is int = 20
mg_StockWarningThreshold is int
Modify the class constructor.
PROCÉDURE Constructor(StockWarningThreshold is int)
mg_StockWarningThreshold = StockWarningThreshold
Go to the project initialization code.
  1. We will modify:
    • the class instantiation. Replace the following line of code:
      AProduct is CProduct
      AProduct is CProduct(20)
      As already mentioned, the Constructor method is automatically called when the class is declared (instantiated). Now the constructor expects a parameter: we must pass the parameter value in the instantiation. In this example, the maximum quantity is set to 20.
    • the call to the method. Replace the following line of code:
      The quantity in stock is 10.
  2. Let's test our changes (Click "GO" in the quick access icons).
  3. The "Debugger trace" pane shows the following data:
    REF-123 My product 10
    Not enough units in stock
To sum up
In this lesson, you have discovered how to create a class, its members, and how to use them.
To learn more about OOP, take a look at the following pages:
Previous LessonTable of contents
Minimum version required
  • Version 2024
Click [Add] to post a comment

Last update: 12/01/2023

Send a report | Local help