SetItem Method

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

Description

In JavaScript, use the aqBytesObj.SetItem method to set a new value for the specified byte. To learn how to do this in other languages, see an example. To get a byte, use the Item property of the aqBytes object.

Declaration

aqBytesObj.SetItem(Index, Value)

aqBytesObj An expression, variable or parameter that specifies a reference to an aqBytes object
Index [in]    Required    Integer    
Value [in]    Required    Integer    
Result None

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Index

Specifies the index of the byte to write. The index ranges between 0 and aqBytes.Count - 1.

Value

Sets a new value for the specified byte.

Result Value

None.

Example

The code below obtains binary data of the specified file, posts the value of the specified byte to the test log and writes a new value for the byte.

JavaScript

function ChangeItem()
{
  Path = "C:\MyFiles\FileName.txt";
  
  // Opens the specified file for reading in binary mode
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead);
  
  // Reads bytes from the file
  readBytes = myFile.ReadBytes();

  // Gets the specified byte and posts its value to the test log
  item = readBytes.Item(10);
  Log.Message("Current value: " + item);
  
  // Writes a new value for the specified byte
  readBytes.SetItem(10, 108);
  
  // Gets the changed byte and posts its value to the test log
  item = readBytes.Item(10);
  Log.Message("Changed value: " + item);
  
  myFile.Close();
}

Python

def ChangeItem():
  Path = "C:\\MyFiles\FileName.txt"
  
  # Opens the file for reading in binary mode
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead)
  
  # Reads bytes from the file
  readBytes = myFile.ReadBytes()
  
  # Gets the specified byte and posts its value to the test log
  item = readBytes.Item[8]
  Log.Message("Current value: " + aqConvert.VarToStr(item))
  
  # Writes a new value for the specified byte
  readBytes.Item[8] = 108

  # Gets the changed byte and posts its value to the test log
  item = readBytes.Item[8]
  Log.Message("Changed value: " + aqConvert.VarToStr(item))
  
  myFile.Close()

VBScript

Sub ChangeItem
  Dim Path, myFile, readBytes, item
  
   Path = "C:\MyFiles\FileName.txt"
  
  ' Opens the specified file for reading in binary mode
  Set myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead)
  
  ' Reads bytes from the file
  Set readBytes = myFile.ReadBytes()
  
  ' Gets the specified byte and posts its value to the test log
  item = readBytes.Item(10)
  Log.Message("Current value: " + aqConvert.VarToStr(item))
  
  ' Writes a new value for the specified byte
  readBytes.Item(10) = 108
  
  ' Gets the changed byte and posts its value to the test log
  item = readBytes.Item(10)
  Log.Message("Changed value: " + aqConvert.VarToStr(item))
End Sub

DelphiScript

procedure ChangeItem;
var Path, myFile, readBytes, item;
begin
  Path := 'C:\MyFiles\FileName.txt';
  
  // Opens the specified file for reading in binary mode
  myFile := aqFile.OpenBinaryFile(Path, aqFile.faRead);
  
  // Reads bytes from the file
  readBytes := myFile.ReadBytes();
  
  // Gets the specified byte and posts its value to the test log
  item := readBytes.Item[10];
  Log.Message('Current value: ' + aqConvert.VarToStr(item));
  
  // Writes a new value for the specified byte
  readBytes.Item[10] := 108;
  
  // Gets the changed byte and posts its value to the test log
  item := readBytes.Item[10];
  Log.Message('Changed value: ' + aqConvert.VarToStr(item));
end;

C++Script, C#Script

function ChangeItem()
{
  Path = "C:\\MyFiles\\FileName.txt";
  
  // Opens the specified file for reading in binary mode
  myFile = aqFile["OpenBinaryFile"](Path, aqFile["faRead"]);
  
  // Reads bytes from the file
  readBytes = myFile["ReadBytes"]();
  
  // Gets the specified byte and posts its value to the test log
  item = readBytes["Item"](10);
  Log["Message"]("Current item: " + aqConvert["VarToStr"](item));
  
  // Writes a new value for the specified byte
  readBytes["Item"](10) = 108;
  
  // Gets the changed byte and posts its value to the test log
  item = readBytes["Item"](10);
  Log["Message"]("Changed item: " + aqConvert["VarToStr"](item));
}

See Also

Item Property
Count Property
aqBytes Object

Highlight search results