PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview
  • Syntax of class constructor
  • Note regarding the constructors
  • Syntax of constructors for the base classes and members
  • Notes
  • Syntax of the Destructor method
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadApple WatchUniversal Windows 10 AppWindows Mobile
Others
Stored procedures
Overview
When creating a class in the code editor, the Constructor and Destructor methods are automatically created by default.
  • The Constructor method, if defined by the developer, is automatically called when instantiating an object. It is used to perform the initialization steps for the object or related to the object (assigning members, checks, ...)
  • The Destructor method, if it is defined by the developer, is automatically called when deleting the object (exit from the procedure in which the object was instantiated). It can be used to free resources for example...
The Constructor and Destructor methods cannot contain the following functions: Event, Timer, Multitask, DnDEvent, DDEEvent, CallDLL32, etc.
Versions 16 and later
Notes:
  • The Constructor methods can be public, protected or private.
  • You have the ability to create some multi-syntax constructors. See Prototype overload for more details.
New in version 16
Notes:
  • The Constructor methods can be public, protected or private.
  • You have the ability to create some multi-syntax constructors. See Prototype overload for more details.
Notes:
  • The Constructor methods can be public, protected or private.
  • You have the ability to create some multi-syntax constructors. See Prototype overload for more details.
Versions 17 and later
AndroidUniversal Windows 10 App This feature is now available for the Android and Windows Phone applications.
iPhone/iPad This feature is now available for the iPhone/iPad applications.
New in version 17
AndroidUniversal Windows 10 App This feature is now available for the Android and Windows Phone applications.
iPhone/iPad This feature is now available for the iPhone/iPad applications.
AndroidUniversal Windows 10 App This feature is now available for the Android and Windows Phone applications.
iPhone/iPad This feature is now available for the iPhone/iPad applications.
Versions 21 and later
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
New in version 21
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
Syntax of class constructor
Declaration of constructor
PROCEDURE [<Access>] Constructor([<Parameters>])
Details of syntax
Versions 16 and later
<Access>: Optional
3 access levels are available:
  • PUBLIC (by default): The constructor is accessible outside the class.
  • PROTECTED: The constructor is accessible in the class and in the derived classes.
  • PRIVATE: The constructor is accessible in the class only.
See Notes about the Constructors for more details.
Note: To modify the scope of the Constructor, you have the ability to use the popup menu on the method in the project explorer.
New in version 16
<Access>: Optional
3 access levels are available:
  • PUBLIC (by default): The constructor is accessible outside the class.
  • PROTECTED: The constructor is accessible in the class and in the derived classes.
  • PRIVATE: The constructor is accessible in the class only.
See Notes about the Constructors for more details.
Note: To modify the scope of the Constructor, you have the ability to use the popup menu on the method in the project explorer.
<Access>: Optional
3 access levels are available:
  • PUBLIC (by default): The constructor is accessible outside the class.
  • PROTECTED: The constructor is accessible in the class and in the derived classes.
  • PRIVATE: The constructor is accessible in the class only.
See Notes about the Constructors for more details.
Note: To modify the scope of the Constructor, you have the ability to use the popup menu on the method in the project explorer.
<Parameters>
Optional parameters of the constructor. If these parameters are specified, they must be passed during the object declaration or during the dynamic instantiation.
Declaration of the object: the constructor is automatically called
<ObjectName> is [object] <Class Name> ([<Parameters>])
Details of syntax
<ObjectName>
Name that identifies the instance of the class.
<Class Name>
Name that identifies the class, defined when creating the class in the code editor.
<Parameters>
Optional parameters of constructor.
Dynamic instantiation: the constructor is automatically called
<ObjectName> is dynamic [object] <ClassName>
<ObjectName> = new <ClassName> ([<Parameters>])
Details of syntax
<ObjectName>
Name that identifies the instance of the class.
<Class Name>
Name that identifies the class, defined when creating the class in the code editor.
<Parameters>
Optional parameters of constructor.
Note regarding the constructors
Versions 16 and later
Scope and access of the Constructors
The Constructor of a class can be public, protected or private.
Inheritance:
  • A class with public Constructor can be inherited.
  • A class with protected Constructor can be inherited.
  • A class with private Constructor can be inherited.
Accessibility of private or protected Constructors:
  • An object of the class cannot be instantiated in a project code.
  • An array whose elements are instances of the class cannot be declared in a project code.
  • A member of the type of the class in a composite variable, a structure or another class cannot be declared in a class code or in a project code.
Important note: Regardless the scope of the Constructor, dynamic objects can always be declared in a project code. If the instantiation (the allocation) of the objects cannot be performed, the declaration of dynamic objects is always possible. Therefore, the following codes are always valid:
// In these two case, the instantiation is not performed during the declaration.
Object1 is dynamic Class1
Object2 is Class1 <-...
To instantiate objects in the code of the project, you have the ability to use a global method of the class.
Example with a private Constructor:
// Global method of ClassWithPrivateConstructor
GLOBAL PROCEDURE GlobalMethodAllocation(...)

o is ClassWithPrivateConstructor
RESULT o
// *****************************************

// Retrieve an object of the class from a project code:

// Solution 1:
o2 is dynamic ClassWithPrivateConstructor
o2 = ClassWithPrivateConstructor.GlobalMethodAllocation(...)

// Solution 2:
o3 is ClassWithPrivateConstructor <- CClassWithPrivateConstructor.GlobalMethodAllocation(...)
Caution: The assignment operator (=) behaves differently with the dynamic objects and with the non-dynamic objects. Using the o2 and o3 objects in the assignments will trigger a different behavior:
// Declaration of a new dynamic object.
o4 is dynamic ClassWithPrivateConstructor

// o2 is a reference to o4
o2 = o4

// The members of o4 are copied into o3
o3 = o4

New in version 16
Scope and access of the Constructors
The Constructor of a class can be public, protected or private.
Inheritance:
  • A class with public Constructor can be inherited.
  • A class with protected Constructor can be inherited.
  • A class with private Constructor can be inherited.
Accessibility of private or protected Constructors:
  • An object of the class cannot be instantiated in a project code.
  • An array whose elements are instances of the class cannot be declared in a project code.
  • A member of the type of the class in a composite variable, a structure or another class cannot be declared in a class code or in a project code.
Important note: Regardless the scope of the Constructor, dynamic objects can always be declared in a project code. If the instantiation (the allocation) of the objects cannot be performed, the declaration of dynamic objects is always possible. Therefore, the following codes are always valid:
// In these two case, the instantiation is not performed during the declaration.
Object1 is dynamic Class1
Object2 is Class1 <-...
To instantiate objects in the code of the project, you have the ability to use a global method of the class.
Example with a private Constructor:
// Global method of ClassWithPrivateConstructor
GLOBAL PROCEDURE GlobalMethodAllocation(...)

o is ClassWithPrivateConstructor
RESULT o
// *****************************************

// Retrieve an object of the class from a project code:

// Solution 1:
o2 is dynamic ClassWithPrivateConstructor
o2 = ClassWithPrivateConstructor.GlobalMethodAllocation(...)

// Solution 2:
o3 is ClassWithPrivateConstructor <- CClassWithPrivateConstructor.GlobalMethodAllocation(...)
Caution: The assignment operator (=) behaves differently with the dynamic objects and with the non-dynamic objects. Using the o2 and o3 objects in the assignments will trigger a different behavior:
// Declaration of a new dynamic object.
o4 is dynamic ClassWithPrivateConstructor

// o2 is a reference to o4
o2 = o4

// The members of o4 are copied into o3
o3 = o4

Scope and access of the Constructors
The Constructor of a class can be public, protected or private.
Inheritance:
  • A class with public Constructor can be inherited.
  • A class with protected Constructor can be inherited.
  • A class with private Constructor can be inherited.
Accessibility of private or protected Constructors:
  • An object of the class cannot be instantiated in a project code.
  • An array whose elements are instances of the class cannot be declared in a project code.
  • A member of the type of the class in a composite variable, a structure or another class cannot be declared in a class code or in a project code.
Important note: Regardless the scope of the Constructor, dynamic objects can always be declared in a project code. If the instantiation (the allocation) of the objects cannot be performed, the declaration of dynamic objects is always possible. Therefore, the following codes are always valid:
// In these two case, the instantiation is not performed during the declaration.
Object1 is dynamic Class1
Object2 is Class1 <-...
To instantiate objects in the code of the project, you have the ability to use a global method of the class.
Example with a private Constructor:
// Global method of ClassWithPrivateConstructor
GLOBAL PROCEDURE GlobalMethodAllocation(...)

o is ClassWithPrivateConstructor
RESULT o
// *****************************************

// Retrieve an object of the class from a project code:

// Solution 1:
o2 is dynamic ClassWithPrivateConstructor
o2 = ClassWithPrivateConstructor.GlobalMethodAllocation(...)

// Solution 2:
o3 is ClassWithPrivateConstructor <- CClassWithPrivateConstructor.GlobalMethodAllocation(...)
Caution: The assignment operator (=) behaves differently with the dynamic objects and with the non-dynamic objects. Using the o2 and o3 objects in the assignments will trigger a different behavior:
// Declaration of a new dynamic object.
o4 is dynamic ClassWithPrivateConstructor

// o2 is a reference to o4
o2 = o4

// The members of o4 are copied into o3
o3 = o4

Syntax of constructors for the base classes and members
Running the constructor of a base class or member
If a base class or a class type member has a constructor, this constructor is automatically called without parameter. You must:
  • assign default values to the parameters of the constructor (or to the parameters of the base class or member)
  • explicitly call the constructor by passing the parameters.
Calling the constructor method to build a class
Constructor <ClassName>(<Parameters>)
Details of syntax
<ClassName>
Name identifying the class.
<Parameters>
Parameters of the constructor.
Calling the constructor to build a member
Constructor <Member name> (<Parameters>)
Details of syntax
<Member Name>
Name identifying the member of the class.
<Parameters>
Parameters of the constructor.
Example
The explicit constructors of the base class or member must be called in first statement of the constructor of the derived class.
//----Declare the BaseClass1 class
BaseClass1 is Class
BaseClass1Member is int
END
//---- Constructor of BaseClass1
PROCEDURE Constructor(Param1)
Info("Constructor of BaseClass1 => " + Param1)
//----Declare the class named BaseClass2
BaseClass2 is Class
BaseClass2Member is int
END
//---- Constructor of BaseClass2
PROCEDURE Constructor(Param1)
Info("Constructor of BaseClass2 => " + Param1)
//---- Declaration of DerivedClass
DerivedClass is Class
// Inheritance of BaseClass1 whose
// Constructor expects a parameter
inherits from ClassBase1
// BaseClass2 member whose
// Constructor expects a parameter
DerivedClassMember is BaseClass2
END
//----Constructor of DerivedClass
PROCEDURE Constructor()
// Explicit call to Constructor
Constructor BaseClass1(10)
Constructor DerivedClassMember(20)
Notes
Default value of members
Each member of a class is set to zero if no constructor is associated with the class.
Syntax of the Destructor method
Declaration of the method
PROCEDURE Destructor <ClassName>()
Details of syntax
<ClassName>
Name identifying the class. The destructor accepts no parameter.
AndroidJava Special case in Java/Android
The concept of destructor does not exist in Java/Android. Therefore, the code found in the destructor of a class will never be run.

An object is freed by the garbage collector when it is no longer referenced in the application. There is no way to foresee the moment when this operation occurs.

To force an object to be freed, you have the ability to assign it to NULL or to use the Free statement.
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 required version
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment