AddVariable Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

The Variables.AddVariable method creates a new variable and adds it to the given Variables collection.

Declaration

VariablesObj.AddVariable(VariableName, VariableType)

VariablesObj An expression, variable or parameter that specifies a reference to a Variables object
VariableName [in]    Required    String    
VariableType [in]    Required    String    
Result None

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

VariableName

The variable name. This name will be used to address the variable in scripts, so it must match the naming rules of the scripting language you use. At that, the names of the project suite and network suite variables must conform to the naming rules of scripting languages in any project that belongs to the suite. The easiest way to make the variable name suitable for any language is to use only alphanumeric (A..Z, a..z, 0..9) and underscore (_) characters in the name and to start the name with a letter.

VariableType

One of the following string values that specify the variable type:

Value Description
Boolean The variable can store boolean values.
Double The variable can store floating-point values and dates.
Integer The variable can store integer values.
Object The variable can store object references.
String The variable can store string values.
Password The variable can store encrypted string values.
Table The variable can store a two-dimensional table of values (a scripting interface to a table variable is provided by the TableVariable object).
DB Table
(note the space between the words)
The variable can be linked to an external data source, such as a database table or recordset, an Excel spreadsheet or CSV file, and returns values from that source (a scripting interface to a database table variable is provided by the DBTableVariable object).

Result Value

None.

Remarks

If the variable with the specified name already exists in the collection, an error occurs.

This method creates a variable without assigning any value to it. To specify the variable value, use one of the following constructions:

JavaScript

VariablesObj.variable_name = value;
// -- or --
VariablesObj.$set("VariableByName", variable_name, value);

JScript

VariablesObj.variable_name = value;
// -- or --
VariablesObj.VariableByName(variable_name) = value;

Python

VariablesObj.variable_name = value
// -- or --
VariablesObj.VariableByName(variable_name) = value

VBScript

VariablesObj.variable_name = value
' -- or --
VariablesObj.VariableByName(variable_name) = value

DelphiScript

VariablesObj.variable_name := value;
// -- or --
VariablesObj.VariableByName[variable_name] := value;

C++Script, C#Script

VariablesObj[variable_name] = value;
// -- or --
VariablesObj["VariableByName"](variable_name) = value;

You cannot use the AddVariable method to create network suite variables in tests while the network suite is running. You can add network suite variables in tests only before the network suite starts or after the network suite execution is finished. You can also create network suite variables at design time. See Network Suite Variables - Overview .

Example

The following code snippet adds a project variable named MyVar of the string type and initializes it:

JavaScript, JScript

Project.Variables.AddVariable("MyVar", "String");
Project.Variables.MyVar = "Hello, world!";

Python

Project.Variables.AddVariable("MyVar", "String")
Project.Variables.MyVar = "Hello, world!"

VBScript

Project.Variables.AddVariable "MyVar", "String"
Project.Variables.MyVar = "Hello, world!"

DelphiScript

Project.Variables.AddVariable('MyVar', 'String');
Project.Variables.MyVar := 'Hello, world!';

C++Script, C#Script

Project["Variables"]["AddVariable"]("MyVar", "String");
Project["Variables"]["MyVar"] = "Hello, world!";

See Also

RemoveVariable Method
VariableExists Method

Highlight search results