Delete Method

Applies to TestComplete 15.44, last modified on November 10, 2022

The object-driven testing (ODT) functionality is deprecated. Do not use it to create new tests. It will be removed from the product in one of the future releases. As an alternative, you can create custom classes in your scripts. For more information, see Alternatives to the ODT functionality.

Description

The Classes.Delete method deletes a class specified by its name from the collection of classes. If the collection does not have a class with the specified name, an error occurs.

Declaration

ClassesObj.Delete(Name)

ClassesObj An expression, variable or parameter that specifies a reference to a Classes object
Name [in]    Required    String    
Result None

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Name

Specifies the name of the class to be deleted.

Result Value

None.

Remarks

You cannot use the Delete method to delete a class if any instances of it exist.

If you use JScript, C#Script or C++Script, you may face a problem when TestComplete cannot delete the specified class using the Delete method. For example, if you run the fooA function below, an error will occur.

tThis happens because JScript does not free local variables after the function execution is finished. For example, in the code snippet below, the instance variable still exists in memory and stores a reference to an object even after the fooA function exits. So, if the variable exists, TestComplete cannot delete the Example class, as it has at least one instance alive. To avoid such problems, we recommend that you free variables forcibly after the function execution is over. Note that you cannot free them while the function in which they are created is being executed.

To free all the variables that were created when the function was being executed, you can call the undocumented CollectGarbage method (in our example, after the fooB function is called).

So, the correct example in this case will look like this:

JScript

function fooB()
{
  var instance = ODT.Classes.New("Example");
}
function fooA(){
  ODT.Classes.Clear();
  ODT.Classes.Declare("Example");
  fooB();
  CollectGarbage();
  ODT.Classes.Delete("Example");
}

C++Script, C#Script

function fooB()
{
  var instance = ODT["Classes"]["New"]("Example");
}
function fooA(){
  ODT["Classes"]["Clear"]();
  ODT["Classes"]["Declare"]("Example");
  fooB();
  CollectGarbage();
  ODT["Classes"]["Delete"]("Example");
}

Example

The code below declares a new class and uses its property to store some temporary data. At the end of the test run, the routine deletes the class from the project.

JavaScript, JScript

function DeleteClassExample()
{
  // Declares a new class
  ODT.Classes.Declare("MyClass");
  var MyClass = ODT.Classes.MyClass;
  // Writes some temporary data to the property
  // of the new class
  MyClass.AddProperty("Temp", "Some temporary data");
  // ...
  
  // Make sure all the instances
  // of the MyClass class have been deleted
  
  // Deletes the class
  ODT.Classes.Delete("MyClass");
}

VBScript

Sub DeleteClassExample()

  ' Declares a new class
  ODT.Classes.Declare("MyClass")
  Set MyClass = ODT.Classes.MyClass
  ' Writes some temporary data to the property
  ' of the new class
  Call MyClass.AddProperty("Temp", "Some temporary data")
  ' ...
  
  ' Make sure all the instances
  ' of the MyClass class have been deleted
  
  ' Deletes the class
  ODT.Classes.Delete("MyClass")

End Sub

DelphiScript

function DeleteClassExample;
var MyClass;
begin

  // Declares a new class
  ODT.Classes.Declare('MyClass');
  MyClass := ODT.Classes.MyClass;
  // Writes some temporary data to the property
  // of the new class
  MyClass.AddProperty('Temp', 'Some temporary data');
  // ...
  
  // Make sure all the instances
  // of the MyClass class have been deleted
  
  // Deletes the class
  ODT.Classes.Delete('MyClass');

end;

C++Script, C#Script

function DeleteClassExample()
{
  // Declares a new class
  ODT["Classes"]["Declare"]("MyClass");
  var MyClass = ODT["Classes"]["MyClass"];
  // Writes some temporary data to the property
  // of the new class
  MyClass["AddProperty"]("Temp", "Some temporary data");
  // ...
  
  // Make sure all the instances
  // of the MyClass class have been deleted
  
  // Deletes the class
  ODT["Classes"]["Delete"]("MyClass");
}

See Also

New Method
Declare Method

Highlight search results