Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the RtlMoveMemory API function to copy part of a two-dimensional array into a one-dimensional array in Visual Basic .NET
DescriptionThis example shows how to use the RtlMoveMemory API function to copy part of a two-dimensional array into a one-dimensional array in Visual Basic .NET.
KeywordsRtlMoveMemory, copy memory, CopyMemory, MoveMemory, array, VB .NET
CategoriesVB.NET, Algorithms, Utilities
 
Enter the rows and columns that you want to copy out of the two-dimensional array. When you click the Copy button, the following code executes. The code copies the values shown in the form's TextBoxes into a two-dimensional array and highlights the entries you selected.

Next the program loops through the rows you selected, using RtlMoveMemory to copy the choosen parts of the two-dimensional array.

Note that this is different from the way the Visual Basic 6 version works because Visual Basic 6 stores two-dimensional arrays in column-major order (it places the items in a column in adjacent memory locations) while Visual Basic .NET stores them in row-major order (it places the items in a row in adjacent memory locations).

 
Private Sub btnCopy_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCopy.Click
    ' Reset all text box colors.
    For Each ctl As Control In Me.Controls
        If TypeOf ctl Is TextBox Then
            ctl.ForeColor = Color.Black
            ctl.BackColor = Color.White
        End If
    Next ctl

    ' Make the two-dimensional array (0 To 3, 0 To 4).
    Dim in_array(3, 4) As Integer
    For r As Integer = 0 To 3
        For c As Integer = 0 To 4
            Dim control_name As String = "TextBox" & (r * 5 _
                + c + 1).ToString()
            Dim txt As TextBox = FindControl(control_name)
            in_array(r, c) = Integer.Parse(txt.Text)
        Next c
    Next r

    ' Color the selected entries.
    Dim row_min As Integer = Integer.Parse(txtRowMin.Text) _
        - 1
    Dim row_max As Integer = Integer.Parse(txtRowMax.Text) _
        - 1
    Dim col_min As Integer = Integer.Parse(txtColMin.Text) _
        - 1
    Dim col_max As Integer = Integer.Parse(txtColMax.Text) _
        - 1
    For r As Integer = row_min To row_max
        For c As Integer = col_min To col_max
            Dim control_num As Integer = r * 5 + c + 1
            Dim field As TextBox = FindControl("TextBox" & _
                control_num.ToString())
            field.BackColor = Color.Black
            field.ForeColor = Color.White
        Next c
    Next r

    ' Copy the data into out_array using MemCopy.
    Dim num_rows As Integer = row_max - row_min + 1
    Dim num_cols As Integer = col_max - col_min + 1
    Dim bytes_per_row As Integer = Len(col_min) * (num_cols)
    Dim out_array(bytes_per_row * num_rows - 1) As Integer
    Dim idx As Integer = 0
    For r As Integer = row_min To row_max
        MoveMemory(out_array(idx), in_array(r, col_min), _
            bytes_per_row)
        idx += num_cols
    Next r

    ' Print the output values.
    Dim result As String = ""
    For i As Integer = 0 To num_rows * num_cols - 1
        result &= out_array(i).ToString("0") & " "
    Next i
    txtResult.Text = result
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated