PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • Overview of an object
  • Syntax for declaring/instantiating an object
  • Declaring/Instantiating an object
  • Declaring/Instantiating a Null object
  • Note: Current object
  • Members and methods
  • Lifespan of an object
  • Reference on a local object
  • Returning an object
  • Objects in the loops
  • Freeing the object
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
Instantiation of an object
Overview of an object
To access a class, the object must be declared as being part of the class to handle, this is called object instantiation.
An instance is an object that belongs to a given class.
To handle an object, you must:
  1. Describe the class.
  2. Instantiate the object.
An object can be passed in parameter to a function or to a procedure.
Note: You have the ability to dynamically instantiate an object to a class. The implementation of polymorphism requires the dynamic instantiation. See Dynamic instantiation for more details.
Example of object: "SourceFile" is object of "File" class. For this object, you have the ability to handle the members named "Name", "Extension", etc.
SourceFile is File object

SourceFile is File
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 and 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 and 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 and 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 for declaring/instantiating an object

Declaring/Instantiating an object

<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 the constructor. See Constructor method for more details.

Declaring/Instantiating a Null object

<ObjectName> is [object] <Class Name> ([<Parameters>]) = NULL
This syntax is used to declare an object without instantiating it. In this case:
  • the <- operator is used to take a reference on the object.
  • the declared variable behaves as if the declaration was a non-dynamic declaration: the = operator performs a copy of the members.
  • the object handled can be returned as return value of the procedure or method.
  • in some advanced cases, the object handled can be destroyed to anticipate the automatic destroy operation.

Note: Current object

The Object keyword can also be used to identify the current object (equivalent to "This" in other languages).

Members and methods

See Using an object for more details.
Lifespan of an object

Reference on a local object

The object is created during its declaration. The object is local by default.
The local objects are not automatically destroyed at the end of the process execution. Only the local reference is freed.
Therefore, if another reference was taken on the object, the object will not be destroyed when the local reference is freed.
Example:
// Declaration code of global variables
gpo is Class1 dynamic
gArr is array of Class1 dynamic

// Any code
o1, o2, o3 are Class1
gpo1 = o1
Add(gArr, o2)

// At the end of process
// o1 is not destroyed because it is referenced by the global variable gpo
// o2 is not destroyed because it was added to the gArr array
// o3 is not destroyed because there is only the local reference

Returning an object

You have the ability to return an object handled by a dynamic or non-dynamic local variable.
Note: In the earlier versions, you only had the ability to return an object handled by a dynamic local variable.

Objects in the loops

In a loop, the objects can be handled by non-dynamic local variables and without explicit allocation.
The objects declared in a loop are reallocated at each iteration of the loop and the reference on the object of the previous iteration is freed:
  • if no other reference was taken, the object is destroyed.
  • if a reference was taken (global variable, addition to an array, and so on), the object is not destroyed.
Example:
// build the array
t is array of Class1 dynamic
FOR I = 1 TO 10
// create an object corresponding to the subscript
o is Class (x)
o.m_nSubscript = I
// add the object into the array
Add(t,o)
END

Freeing the object

The objects used by non-dynamic local variables can be explicitly destroyed by the DELETE keyword to anticipate the automatic destruction.
Note: This syntax is an advanced syntax and it should be used in specific cases only. In most cases, the destroy operation is automatic.
Reminder: In most cases, the use of the DELETE keyword is not required. The WLanguage automatically destroys the objects that are no longer used by any variable. The most common cases are:
  • destroying a set of objects that are referenced in a cyclical way
  • freeing the resources used by the instance (sockets, files, ...) in advance via the execution of the destructor.
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.
Minimum required version
  • Version 14
This page is also available for…
Comments
Click [Add] to post a comment