AddMethod Method

Applies to TestComplete 15.70, last modified on December 17, 2024

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 Object.AddMethod method creates a new method in the object and returns this method as the Method object.

Declaration

ObjectObj.AddMethod(MethodName, ScriptProc)

ObjectObj An expression, variable or parameter that specifies a reference to an Object object
MethodName [in]    Required    String    
ScriptProc [in]    Required    String    
Result A Method object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

MethodName

Specifies the name of the new method. This name must be unique within the class. If the object already contains a method or property with the specified name, an error will occur. Since the method name is used to call the method in scripts, it must match the naming rules of the selected scripting language (it must be a valid identifier).

ScriptProc

Specifies a script routine that will be used as the method. ScriptProc must be specified in the format unit_name.routine_name. Both unit_name and routine_name are required. The unit_name specifies the unit where the routine is located. If this unit differs from the class unit, you should specify it in the USEUNIT (uses) clauses of the class unit (see Calling Routines and Variables Declared in Another Unit).

Result Value

The new method represented as a Method object.

Remarks

Note that a new method is added to the Object_Obj object only, it is not added to the class on which Object_Obj is based or to other objects that are based on this class.

To refer to the object instance that the given method belongs to in the routine code, use the keyword Self in DelphiScript or This in VBScript, JScript, C++Script or C#Script (note the first capital):

JavaScript, JScript

function Cls_Method ()
{
  ...
  This.Owner.Owner.Execute();
  ...
}

VBScript

Sub Cls_Method
  ...
  Call This.Owner.Owner.Execute
  ...
End Sub

DelphiScript

procedure Cls_Method;
begin
  ...
  Self.Owner.Owner.Execute;
  ...
end;

C++Script, C#Script

function Cls_Method ()
{
  ...
  This["Owner"]["Owner"]["Execute"]();
  ...
}

Example

The following code snippet creates an object that stores an instance of the NewClass class and adds a new method to the created object.

JavaScript, JScript

function ObjectSample()
{
  var NewObject, TestMethod;
  // Creates an Object that contains an instance of the ODT class
  NewObject = ODT.Classes.New("NewClass");
  // Adds a new "TestMethod" method to the object
  TestMethod = NewObject.AddMethod("TestMethod", "Unit1.Test");
  // Calls the object’s methods and processes the object’s properties
  NewObject.Run();

}

VBScript

Sub ObjectSample

  ' Creates an Object that contains an instance of the ODT class
  Set NewObject = ODT.Classes.New("NewClass")
  ' Adds a new "TestMethod" method to the object
  Set TestMethod = NewObject.AddMethod("TestMethod", "Unit1.Test")
  ' Calls the object’s methods and processes the object’s properties
  NewObject.Run

End Sub

DelphiScript

procedure ObjectSample();
var NewObject, TestMethod;
begin
  // Creates an Object that contains an instance of the ODT class
  NewObject := ODT.Classes.New('NewClass');
  // Adds a new "TestMethod" method to the object
  TestMethod := NewObject.AddMethod('TestMethod', 'Unit1.Test');
  // Calls the object’s methods and processes the object’s properties
  NewObject.Run;

end;

C++Script, C#Script

function ObjectSample()
{
  var NewObject, TestMethod;
  // Creates an Object that contains an instance of the ODT class
  NewObject = ODT["Classes"]["New"]("NewClass");
  // Adds a new "TestMethod" method to the object
  TestMethod = NewObject["AddMethod"]("TestMethod", "Unit1.Test");
  // Calls the object’s methods and processes the object’s properties
  NewObject["Run"]();

}

See Also

Method Object
AddMethod Method
AddProperty Method
DeleteMethod Method
Methods Property

Highlight search results