SetOptionByIndex Method

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

Description

Use this method to set a new value for an option of the current section. The option is identified by its index in the option collection of the current section. To get the number of options in this collection, use the OptionCount property. If the option with the specified index does not exist, an error occurs.

Declaration

ProgObj.SetOptionByIndex(IndexNewValue)

ProgObj An expression, variable or parameter that specifies a reference to one of the objects listed in the Applies To section
Index [in]    Required    Integer    
NewValue [in]    Required    Variant    
Result None

Applies To

The method is applied to the following objects:

Parameters

The method has the following parameters:

Index

Specifies the index of the option whose value you want to set. The index is zero-based, that is, the first option has index 0, the second - 1, and so on. Index of the last option is OptionCount-1.

NewValue

Specifies the new value of the specified option.

Result Value

None.

Remarks

Be careful when modifying the system registry using the Section object methods. Incorrect modifications made to the registry may affect the functionality of your operating system and the applications installed.

When working with the registry, the Section.SetOptionByIndex method lets you modify or create values of the REG_DWORD, REG_SZ and REG_EXPAND_SZ types only. The REG_BINARY and REG_MULTI_SZ value types are not supported.

Example

The following example demonstrates how to use the GetOptionByIndex method to obtain the registry key value and the SetOptionByIndex method to modify the value.

JavaScript, JScript

function Test()
{

  var Key, ValueName, OldValue, NewValue, Count, Indx;

  // Gets an object for the Windows system registry key
  Key = Storages.Registry("TestKey\\SubKey", HKEY_CURRENT_USER);
  // Specifies the index of the value
  Indx = 0;

  // Checks whether the key contains any values
  if (Key.OptionCount > 0)
    {
    // Checks whether the key contains the value with the specified index
    if (Indx < Key.OptionCount)
      {
      ValueName = Key.GetOptionName(Indx);

      // Obtains the old value by its index
      OldValue = Key.GetOptionByIndex(Indx, "not specified");
      NewValue = "New Value";

      // Sets a new value
      Key.SetOptionByIndex(Indx, NewValue);
      Log.Message("The" + ValueName + " value has been changed from " + OldValue + " to " + NewValue);
      }
    else
      Log.Error("The value with the specified index does not exist.");
    }
  else
    Log.Error("The key contains no values");

}

Python

def Test():

  # Gets an object for the Windows system registry key
  Key = Storages.Registry("TestKey\\SubKey", HKEY_CURRENT_USER)
  # Specifies the index of the value
  Indx = 0

  # Checks whether the key contains any values
  if (Key.OptionCount > 0):
    # Checks whether the key contains the value with the specified index
    if (Indx < Key.OptionCount):
      ValueName = Key.GetOptionName(Indx)

      # Obtains the old value by its index
      OldValue = Key.GetOptionByIndex(Indx, "not specified")
      NewValue = "New Value"

      # Sets a new value
      Key.SetOptionByIndex(Indx, NewValue)
      Log.Message("The" + ValueName + " value has been changed from " + str(OldValue) + " to " + NewValue)
    else:
      Log.Error("The value with the specified index does not exist.")
  else:
    Log.Error("The key contains no values")

VBScript

Sub Test

  Dim Key, ValueName, OldValue, NewValue, Count, Indx
  ' Gets an object for the Windows system registry key
  Set Key = Storages.Registry("TestKey\SubKey", HKEY_CURRENT_USER)
  ' Specifies the index of the value
  Indx = 0
  ' Checks whether the key contains any values
  If Key.OptionCount > 0 Then
    ' Checks whether the key contains the value with the specified index

    If Indx < Key.OptionCount Then

      ValueName = Key.GetOptionName(Indx)
      ' Obtains the old value by its index
      OldValue = Key.GetOptionByIndex(Indx, "not specified")
      NewValue = "New Value"
      ' Sets a new value
      Call Key.SetOptionByIndex(Indx, NewValue)
      Log.Message("The" & ValueName & " value has been changed from " & OldValue & " to " & NewValue)

    Else
      Log.Error("The value with the specified index does not exist.")
    End If
  Else
    Log.Error("The key contains no values")
  End If

End Sub

DelphiScript

procedure Test();
var Key, ValueName, OldValue, NewValue, Count, Indx;

begin

  // Gets an object for the Windows system registry key
  Key := Storages.Registry('TestKey\SubKey', HKEY_CURRENT_USER);
  // Specifies the index of the value
  Indx := 0;

  // Checks whether the key contains any values
  if Key.OptionCount > 0 then
    begin
    // Checks whether the key contains the value with the specified index
    if Indx < Key.OptionCount then
      begin
      ValueName := Key.GetOptionName(Indx);

      // Obtains the old value by its index
      OldValue := Key.GetOptionByIndex(Indx, 'not specified');
      NewValue := 'New Value';

      // Sets a new value
      Key.SetOptionByIndex(Indx, NewValue);
      Log.Message('The' + ValueName + ' value has been changed from ' + OldValue + ' to ' + NewValue);
      end
    else
      Log.Error('The value with the specified index does not exist.');
    end
  else
    Log.Error('The key contains no values');

end;

C++Script, C#Script

function Test()
{

  var Key, ValueName, OldValue, NewValue, Count, Indx;

  // Gets an object for the Windows system registry key
  Key = Storages["Registry"]("TestKey\\SubKey", HKEY_CURRENT_USER);
  // Specifies the index of the value
  Indx = 0;

  // Checks whether the key contains any values
  if (Key["OptionCount"] > 0)
    {
    // Checks whether the key contains the value with the specified index
    if (Indx < Key["OptionCount"])
      {
      ValueName = Key["GetOptionName"](Indx);

      // Obtains the old value by its index
      OldValue = Key["GetOptionByIndex"](Indx, "not specified");
      NewValue = "New Value";

      // Sets a new value
      Key["SetOptionByIndex"](Indx, NewValue);
      Log["Message"]("The" + ValueName + " value has been changed from " + OldValue + " to " + NewValue);
      }
    else
      Log["Error"]("The value with the specified index does not exist.");
    }
  else
    Log["Error"]("The key contains no values");

}

See Also

SetOption Method
GetOptionByIndex Method
OptionCount Property

Highlight search results