GetOptionByIndex Method

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

Description

Returns the value of an option in the current section. The sought-for 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.

Note: The GetOptionByIndex method cannot be used to get a registry key value marked as (Default). To get this value, use the GetOption method and specify an empty string in the OptionName parameter.

Declaration

ProgObj.GetOptionByIndex(Index, DefaultValue)

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    
DefaultValue [in]    Required    Variant    
Result Variant

Applies To

The method is applied to the following objects:

Parameters

The method has the following parameters:

Index

Specifies the index of the desired option. 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.

DefaultValue

The DefaultValue parameter is ignored. It is reserved for future implementation.

Result Value

The value of the specified option.

Remarks

To determine the specified option’s type, TestComplete uses the specified default value’s type. For instance, if you specify a string in the DefaultValue parameter, TestComplete considers the option a string type.

When working with the registry, the Section.GetOptionByIndex method lets you get 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

GetOption Method
GetSubSectionByIndex Method
OptionCount Property
SetOptionByIndex Method

Highlight search results