' Example of the Buffer.BlockCopy method.
Imports System
Imports Microsoft.VisualBasic
Module BlockCopyDemo
' Display the array contents in hexadecimal.
Sub DisplayArray( arr As Array, 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.
Console.Write( "{0,5}:", name )
For loopX = arr.Length - 1 to 0 Step -1
Console.Write( format, arr( loopX ) )
Next loopX
Console.WriteLine( )
End Sub
Sub Main( )
' These are source and destination arrays for BlockCopy.
Dim src( ) As Short = { 258, 259, 260, 261, 262, 263, 264, _
265, 266, 267, 268, 269, 270 }
Dim dest( ) As Long = { 17, 18, 19, 20 }
Console.WriteLine( "This example of the " & vbCrLf & _
"Buffer.BlockCopy( Array, Integer, Array, " & _
"Integer, Integer ) " & vbCrLf & "method generates " & _
"the following output." & vbCrLf & "Note: The " & _
"arrays are displayed from right to left." & vbCrLf )
Console.WriteLine( " Initial values of arrays:" & vbCrLf )
' Display the values of the arrays.
DisplayArray( src, "src" )
DisplayArray( dest, "dest" )
' Copy two regions of source array to destination array,
' and two overlapped copies from source to source.
Console.WriteLine( vbCrLf & _
" Call these methods: " & vbCrLf & vbCrLf & _
" Buffer.BlockCopy( src, 5, dest, 7, 6 )," & vbCrLf & _
" Buffer.BlockCopy( src, 16, dest, 22, 5 )," & vbCrLf & _
" (these overlap source and destination)" & vbCrLf & _
" Buffer.BlockCopy( src, 4, src, 5, 7 )," & vbCrLf & _
" Buffer.BlockCopy( src, 16, src, 15, 7 )." & vbCrLf )
Console.WriteLine( " Final values of arrays:" & vbCrLf )
Buffer.BlockCopy( src, 5, dest, 7, 6 )
Buffer.BlockCopy( src, 16, dest, 22, 5 )
Buffer.BlockCopy( src, 4, src, 5, 7 )
Buffer.BlockCopy( src, 16, src, 15, 7 )
' Display the arrays again.
DisplayArray( src, "src" )
DisplayArray( dest, "dest" )
End Sub
End Module
' This example of the
' Buffer.BlockCopy( Array, Integer, Array, Integer, Integer )
' method generates the following output.
' Note: The arrays are displayed from right to left.
'
' Initial values of arrays:
'
' src: 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
' dest: 0000000000000014 0000000000000013 0000000000000012 0000000000000011
'
' Call these methods:
'
' Buffer.BlockCopy( src, 5, dest, 7, 6 ),
' Buffer.BlockCopy( src, 16, dest, 22, 5 ),
' (these overlap source and destination)
' Buffer.BlockCopy( src, 4, src, 5, 7 ),
' Buffer.BlockCopy( src, 16, src, 15, 7 ).
'
' Final values of arrays:
'
' src: 010E 010D 0D01 0C01 0B01 0A09 0108 0701 0601 0501 0404 0103 0102
' dest: 00000000000C010B 010A000000000013 0000000701060105 0100000000000011