New Method

Applies to TestComplete 15.47, last modified on January 20, 2023

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.New method creates a new instance of the class whose name is specified by the Name parameter, and returns this instance as an Object object.

Declaration

ClassesObj.New(Name)

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

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Name

Specifies the name of the class whose instance should be created.

Result Value

The instance of the specified class represented as an Object object.

Note: The class with the specified name must be previously declared using the Declare method. If this class is not declared, an error occurs.

Remarks

In your Visual Basic .NET Connected Applications, you should use the Classes.New_ method instead of Classes.New. This is because of peculiarities of the Visual Basic .NET language, which does not allow the Classes.New notation.

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

Declare Method
New_ Method
Object object
Creating Connected Applications in Visual Basic .NET

Highlight search results