Buffer.SetByte Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Assigns a specified value to a byte at a particular location in a specified array.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Sub SetByte ( _
    array As Array, _
    index As Integer, _
    value As Byte _
)
[SecuritySafeCriticalAttribute]
public static void SetByte(
    Array array,
    int index,
    byte value
)

Parameters

Exceptions

Exception Condition
ArgumentException

array is not a primitive.

ArgumentNullException

array is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is negative or greater than the length of array.

Remarks

array must be an array of primitives.

Examples

The following code example assigns values to bytes at particular locations within arrays using the SetByte method.

' Example of the Buffer.SetByte method.

Module Example

   ' Display the array contents in hexadecimal.
   Sub DisplayArray(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal arr As Array, ByVal name As String)

      ' Get the array element width; format the formatting string.
      Dim loopX As Integer
      Dim elemWidth As Integer = _
          Buffer.ByteLength(arr) / arr.Length
      Dim format As String = _
          String.Format(" {{0:X{0}}}", 2 * elemWidth)

      ' Display the array elements from right to left.
      outputBlock.Text &= String.Format("{0,7}:", name)
      For loopX = arr.Length - 1 To 0 Step -1
         outputBlock.Text &= String.Format(format, arr(loopX))
      Next loopX
      outputBlock.Text &= vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' These are the arrays to be modified with SetByte.
      ' This allocates 10 elements for shorts and 3 elements
      ' for longs in Visual Basic.
      Dim shorts(9) As Short
      Dim longs(2) As Long

      outputBlock.Text &= "This example of the " & _
          "Buffer.SetByte( Array, Integer, Byte ) " & vbCrLf & _
          "method generates the following output." & vbCrLf & _
          "Note: The arrays are displayed from right to left." & _
          vbCrLf & vbCrLf
      outputBlock.Text &= "  Initial values of arrays:" & vbCrLf & vbCrLf

      ' Display the initial values of the arrays.
      DisplayArray(outputBlock, shorts, "shorts")
      DisplayArray(outputBlock, longs, "longs")

      ' Copy two regions of source array to destination array,
      ' and two overlapped copies from source to source.
      outputBlock.Text &= vbCrLf & _
          "  Array values after setting byte 3 = 25, " & vbCrLf & _
          "  byte 6 = 64, byte 12 = 121, and byte 17 = 196:" & vbCrLf & vbCrLf

      Buffer.SetByte(shorts, 3, 25)
      Buffer.SetByte(shorts, 6, 64)
      Buffer.SetByte(shorts, 12, 121)
      Buffer.SetByte(shorts, 17, 196)
      Buffer.SetByte(longs, 3, 25)
      Buffer.SetByte(longs, 6, 64)
      Buffer.SetByte(longs, 12, 121)
      Buffer.SetByte(longs, 17, 196)

      ' Display the arrays again.
      DisplayArray(outputBlock, shorts, "shorts")
      DisplayArray(outputBlock, longs, "longs")
   End Sub
End Module

' This example of the Buffer.SetByte( Array, Integer, Byte )
' method generates the following output.
' Note: The arrays are displayed from right to left.
' 
'   Initial values of arrays:
' 
'  shorts: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
'   longs: 0000000000000000 0000000000000000 0000000000000000
' 
'   Array values after setting byte 3 = 25,
'   byte 6 = 64, byte 12 = 121, and byte 17 = 196:
' 
'  shorts: 0000 C400 0000 0079 0000 0000 0040 0000 1900 0000
'   longs: 000000000000C400 0000007900000000 0040000019000000
// Example of the Buffer.SetByte method.
using System;

class Example
{
   // Display the array contents in hexadecimal.
   public static void DisplayArray(System.Windows.Controls.TextBlock outputBlock, Array arr, string name)
   {
      // Get the array element width; format the formatting string.
      int elemWidth = Buffer.ByteLength(arr) / arr.Length;
      string format = String.Format(" {{0:X{0}}}", 2 * elemWidth);

      // Display the array elements from right to left.
      outputBlock.Text += String.Format("{0,7}:", name);
      for (int loopX = arr.Length - 1; loopX >= 0; loopX--)
         outputBlock.Text += String.Format(format, arr.GetValue(loopX));
      outputBlock.Text += "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // These are the arrays to be modified with SetByte.
      short[] shorts = new short[10];
      long[] longs = new long[3];

      outputBlock.Text += "This example of the " +
          "Buffer.SetByte( Array, int, byte ) \n" +
          "method generates the following output.\n" +
          "Note: The arrays are displayed from right to left.\n" + "\n";
      outputBlock.Text += "  Initial values of arrays:\n" + "\n";

      // Display the initial values of the arrays.
      DisplayArray(outputBlock, shorts, "shorts");
      DisplayArray(outputBlock, longs, "longs");

      // Copy two regions of source array to destination array,
      // and two overlapped copies from source to source.
      outputBlock.Text += "\n" +
          "  Array values after setting byte 3 = 25, \n" +
          "  byte 6 = 64, byte 12 = 121, and byte 17 = 196:\n" + "\n";

      Buffer.SetByte(shorts, 3, 25);
      Buffer.SetByte(shorts, 6, 64);
      Buffer.SetByte(shorts, 12, 121);
      Buffer.SetByte(shorts, 17, 196);
      Buffer.SetByte(longs, 3, 25);
      Buffer.SetByte(longs, 6, 64);
      Buffer.SetByte(longs, 12, 121);
      Buffer.SetByte(longs, 17, 196);

      // Display the arrays again.
      DisplayArray(outputBlock, shorts, "shorts");
      DisplayArray(outputBlock, longs, "longs");
   }
}

/*
This example of the Buffer.SetByte( Array, int, byte )
method generates the following output.
Note: The arrays are displayed from right to left.

  Initial values of arrays:

 shorts: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
  longs: 0000000000000000 0000000000000000 0000000000000000

  Array values after setting byte 3 = 25,
  byte 6 = 64, byte 12 = 121, and byte 17 = 196:

 shorts: 0000 C400 0000 0079 0000 0000 0040 0000 1900 0000
  longs: 000000000000C400 0000007900000000 0040000019000000
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference