Declare Method

Applies to TestComplete 14.71, last modified on April 22, 2021

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

Creates a new class with the specified Name and returns the new class as the Class object. The new class is placed at the end of the collection of classes.

Declaration

ClassesObj.Declare(Name)

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

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Name

Specifies the name of the new class. This name must be unique within the Classes collection. If the collection already holds a class with the specified name, an error will occur.

Result Value

The new class represented as a Class object.

Remarks

After you create a class, you can add methods and properties to it using the AddMethod and AddProperty methods of the Class object.

Note: Note that Declare creates only the class declaration. It does not create an instance of the new class. To create an instance, call the Classes.New method.

Example

The code below declares a class, adds two properties to it, creates a new instance of the class and adds one more property to the instance. After that, the routine obtains the total number of properties that belong to the instance and posts this number to the test log.

JavaScript, JScript

function NewInstanceExample()
{
  // Declares a new class
  ODT.Classes.Declare("MyClass");
  var MyClass = ODT.Classes.MyClass;
  
  // Adds two properties to the class
  MyClass.AddProperty("Property1", 123);
  MyClass.AddProperty("Property2", "My text.");
  
  // Creates an instance of the declared class
  var ClassInstance = ODT.Classes.New("MyClass");
  // Adds a new property to the instance
  ClassInstance.AddProperty("Property3", false);
  
  // Obtains the number of the instance's properties
  var PropNum = ClassInstance.PropertyCount;
  Log.Message(PropNum); // 3
}

VBScript

Sub NewInstanceExample()

  ' Declares a new class
  ODT.Classes.Declare("MyClass")
  Set MyClass = ODT.Classes.MyClass
  
  ' Adds two properties to the class
  Call MyClass.AddProperty("Property1", 123)
  Call MyClass.AddProperty("Property2", "My text.")
  
  ' Creates an instance of the declared class
  Set ClassInstance = ODT.Classes.New("MyClass")
  ' Adds a new property to the instance
  Call ClassInstance.AddProperty("Property3", false)
  
  ' Obtains the number of the instance's properties
  PropNum = ClassInstance.PropertyCount
  Log.Message(PropNum) ' 3

End Sub

DelphiScript

function NewInstanceExample;
var MyClass, ClassInstance, PropNum;
begin

  // Declares a new class
  ODT.Classes.Declare('MyClass');
  MyClass := ODT.Classes.MyClass;
  
  // Adds two properties to the class
  MyClass.AddProperty('Property1', 123);
  MyClass.AddProperty('Property2', 'My text.');
  
  // Creates an instance of the declared class
  ClassInstance := ODT.Classes.New('MyClass');
  // Adds a new property to the instance
  ClassInstance.AddProperty('Property3', false);
  
  // Obtains the number of the instance's properties
  PropNum := ClassInstance.PropertyCount;
  Log.Message(PropNum); // 3

end;

C++Script, C#Script

function NewInstanceExample()
{
  // Declares a new class
  ODT["Classes"]["Declare"]("MyClass");
  var MyClass = ODT["Classes"]["MyClass"];
  
  // Adds two properties to the class
  MyClass["AddProperty"]("Property1", 123);
  MyClass["AddProperty"]("Property2", "My text.");
  
  // Creates an instance of the declared class
  var ClassInstance = ODT["Classes"]["New"]("MyClass");
  // Adds a new property to the instance
  ClassInstance["AddProperty"]("Property3", false);
  
  // Obtains the number of the instance's properties
  var PropNum = ClassInstance["PropertyCount"];
  Log["Message"](PropNum); // 3
}

See Also

New Method
NewArray Method
AddProperty Method
AddMethod Method

Highlight search results