Name Property

Applies to TestComplete 14.40, 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

Use the Variable.Name property to get the name of the specified variable.

Declaration

VariableObj.Name

Read-Write Property String
VariableObj An expression, variable or parameter that specifies a reference to a Variable object

Applies To

The property is applied to the following object:

Property Value

A string that holds the variable name.

Remarks

When changing this property, keep in mind that the variable name must be unique within the data group. If the group already contains another variable with the same name, an error will occur.

Example

The following example demonstrates how you can get the name of the specified variable and then change the name.

JavaScript, JScript

function Test()
{

  var Group, MyVar, OldName, NewName;

  // Obtains the data group
  Group = ODT.Data.Groups("MyGroup");
  // Obtains the variable
  MyVar = Group.Variables("MyVar");
  // Obtains the variable’s name
  OldName = MyVar.Name;
  // Specifies a new name for the variable
  NewName = "MyVariable";

  // Checks if a variable with such a name already exists in the group
  for (var i = 0; i < Group.VariableCount; i++)
    {
    if (Group.Variables(i).Name == NewName)
      {
      Log.Warning("Cannot rename the " + OldName + " variable. The variable with the " + NewName + " name already exists.");
      return;
      }
    }
  // Renames the variable
  MyVar.Name = NewName;
  Log.Message("The " + OldName + " variable has been renamed to " + NewName);

}

VBScript

Sub Test

  Dim Group, MyVar, OldName, NewName

  ' Obtains the data group
  Set Group = ODT.Data.Groups("MyGroup")
  ' Obtains the variable
  Set MyVar = Group.Variables("MyVar")
  ' Obtains the variable’s name
  OldName = MyVar.Name
  ' Specifies a new name for the variable
  NewName = "MyVariable"

  ' Checks if a variable with such a name already exists in the group
  For i = 0 To Group.VariableCount - 1
    If Group.Variables(i).Name = NewName Then
      Log.Warning("Cannot rename the " & OldName & " variable. The variable with the " & NewName & " name already exists.")
      Exit Sub
    End If
  Next

  ' Renames the variable
  MyVar.Name = NewName
  Log.Message("The " & OldName & " variable has been renamed to " & NewName)

End Sub

DelphiScript

procedure Test();
var Group, MyVar, OldName, NewName, i;

begin

  // Obtains the data group
  Group := ODT.Data.Groups('MyGroup');
  // Obtains the variable
  MyVar := Group.Variables('MyVar');
  // Obtains the variable’s name
  OldName := MyVar.Name;
  // Specifies a new name for the variable
  NewName := 'MyVariable';

  // Checks if a variable with such a name already exists in the group
  for i := 0 to Group.VariableCount - 1 do
    begin
    if Group.Variables(i).Name = NewName then
      begin
      Log.Warning('Cannot rename the ' + OldName + ' variable. The variable with the ' + NewName + ' name already exists.');
      exit;
      end;
    end;
  // Renames the variable
  MyVar.Name := NewName;
  Log.Message('The ' + OldName + ' variable has been renamed to ' + NewName);

end;

C++Script, C#Script

function Test()
{

  var Group, MyVar, OldName, NewName;

  // Obtains the data group
  Group = ODT["Data"]["Groups"]("MyGroup");
  // Obtains the variable
  MyVar = Group["Variables"]("MyVar");
  // Obtains the variable’s name
  OldName = MyVar["Name"];
  // Specifies a new name for the variable
  NewName = "MyVariable";

  // Checks if a variable with such a name already exists in the group
  for (var i = 0; i < Group["VariableCount"]; i++)
    {
    if (Group["Variables"](i)["Name"] == NewName)
      {
      Log["Warning"]("Cannot rename the " + OldName + " variable. The variable with the " + NewName + " name already exists.");
      return;
      }
    }
  // Renames the variable
  MyVar["Name"] = NewName;
  Log["Message"]("The " + OldName + " variable has been renamed to " + NewName);

}

See Also

Value Property
Object Object

Highlight search results