none
Passing 2-D Array as Subroutine Argument

    Question

  • Is it possible in VB2005 to pass a two-dimensional array as an argument to a subroutine?  I have an app with several integer arrays (with different numbers of rows and columns) and at various points I want to sort one of these arrays ... but it is not clear to me how to do this.  Can someone direct me to an article that discusses this or give me an example?

     

    Thanks.

     

     

    Saturday, July 28, 2007 4:05 AM

Answers

  • Hi,

     

    You pass an entire array regardless of how many dimensions.

     

    I'm trying to do a 2D array sort here where each row is created as a STRING like>>

     

    "7,10,5,9"

     

    What i am now having trouble thinking about is this;

    How to split the above string THEN put it back into each row of the original array.

    I know the string.split method but I guess you need to somehow  do one row at a time?

     

     

    I'm tired, was working last night.

    Not had much sleep.

    Anyone help on this?

     

    Add 1 button to fire this lot.>>

     

    Please note: this 2D array sort routine is based on a character ( not a value sort ) algorithim and sorts in ascending order.

     

    Public Class Form1

     

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    Dim my2dArray(3, 3) As String

    'Row1

    my2dArray(0, 0) = "7"

    my2dArray(0, 1) = "16"

    my2dArray(0, 2) = "9"

    my2dArray(0, 3) = "5"

    'Row2

    my2dArray(1, 0) = "6"

    my2dArray(1, 1) = "8"

    my2dArray(1, 2) = "14"

    my2dArray(1, 3) = "12"

    'Row3

    my2dArray(2, 0) = "4"

    my2dArray(2, 1) = "10"

    my2dArray(2, 2) = "13"

    my2dArray(2, 3) = "15"

    'Row4

    my2dArray(3, 0) = "11"

    my2dArray(3, 1) = "1"

    my2dArray(3, 2) = "3"

    my2dArray(3, 3) = "2"

    'Pass the array.

    Call sortMe(my2dArray)

    End Sub

     

     

    'As this is a Sub

    'ByRef will SORT the original 2D array passed to it of any dimensions.

    Private Sub sortMe(ByRef a2dArray As Array)

     

    'Put the data into a 1d array of type STRING

    Dim rowString(UBound(a2dArray, 1)) As String

    For rows As Integer = 0 To UBound(a2dArray, 1)

    For columns As Integer = 0 To UBound(a2dArray, 2)

    If columns = UBound(a2dArray, 2) Then

    rowString(rows) &= a2dArray(rows, columns).ToString

    Else

    rowString(rows) &= a2dArray(rows, columns).ToString & ","

    End If

    Next

    Next

     

     

    'Sort the rowStrings in ascending order.

    Dim swapString As String

    Dim outString As String = ""

    For check1 As Integer = 0 To UBound(rowString)

    For check2 As Integer = 0 To UBound(rowString)

    If rowString(check1) < rowString(check2) Then

    swapString = rowString(check2)

    rowString(check2) = rowString(check1)

    rowString(check1) = swapString

    End If

    Next

    Next

     

    'Generate the outString which is used for testing.>>>>

    For check1 As Integer = 0 To UBound(rowString)

    outString &= rowString(check1) & vbNewLine

    Next

    'Used for testing.>>>>

    MessageBox.Show(outString)

     

     

    'Put them back into the original 2dArray.

    'Now I'm stuck, can anyone help please?

    ' I'm tired as I was working last night. :-(

    For row As Integer = 0 To UBound(a2dArray, 1)

    For column As Integer = 0 To UBound(a2dArray, 2)

    ' ?

    Next

    Next

     

     

    End Sub

    End Class

     

     

     

    Regards,

     

    S_DS

     

    Saturday, July 28, 2007 4:14 PM

All replies

  • Pass it with commas to indicate the number of dimensions: A( , ) for two dimension, A( , , ) for three dimensions etc.

    Saturday, July 28, 2007 9:33 AM
  • If I understand your question correct, you want to passvarious object arrays to a subroutine, which is easy as long as you know what type the subroutine should expect.

     

    If you want to pass two-dimensional integer arrays, you must decide on, how you want to store them - most preferrably in a collection:

     

    Code Snippet

    Dim myCol As New System.Collections.Generic.List (Of Integer())

     

    myCol.Add New Integer(){1, 2, 3, 4, 5}

    myCol.Add New Integer(){6, 7, 8, 9, 10}

     

    myFunction(myCol)

     

    Function myFunction(ByVal IntArray As System.Collections.Generic.List (Of Integer()))

    For Each objInt As Integer() In IntArray

    'Do something

    Next

    End Function

     

    Or a custom type:

     

    Code Snippet

    Public Class myCol

    Public IntArray As Integer()

    Public Sub New(ByVal IntArray As Integer())

    Me.IntArray = IntArray

    End sub

    End Class

     

    Dim myCol As myCol() = New myCol(){New myCol(New Integer(){1, 2, 3, 4, 5})}

     

    myFunction(myCol)

     

    Function myFunction(ByVal IntArray As myCol())

    For Each objInt As myCol In IntArray

    'Do something with objInt.IntArray

    Next

    End Function

     

    Saturday, July 28, 2007 11:18 AM
  • Hi,

     

    You pass an entire array regardless of how many dimensions.

     

    I'm trying to do a 2D array sort here where each row is created as a STRING like>>

     

    "7,10,5,9"

     

    What i am now having trouble thinking about is this;

    How to split the above string THEN put it back into each row of the original array.

    I know the string.split method but I guess you need to somehow  do one row at a time?

     

     

    I'm tired, was working last night.

    Not had much sleep.

    Anyone help on this?

     

    Add 1 button to fire this lot.>>

     

    Please note: this 2D array sort routine is based on a character ( not a value sort ) algorithim and sorts in ascending order.

     

    Public Class Form1

     

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    Dim my2dArray(3, 3) As String

    'Row1

    my2dArray(0, 0) = "7"

    my2dArray(0, 1) = "16"

    my2dArray(0, 2) = "9"

    my2dArray(0, 3) = "5"

    'Row2

    my2dArray(1, 0) = "6"

    my2dArray(1, 1) = "8"

    my2dArray(1, 2) = "14"

    my2dArray(1, 3) = "12"

    'Row3

    my2dArray(2, 0) = "4"

    my2dArray(2, 1) = "10"

    my2dArray(2, 2) = "13"

    my2dArray(2, 3) = "15"

    'Row4

    my2dArray(3, 0) = "11"

    my2dArray(3, 1) = "1"

    my2dArray(3, 2) = "3"

    my2dArray(3, 3) = "2"

    'Pass the array.

    Call sortMe(my2dArray)

    End Sub

     

     

    'As this is a Sub

    'ByRef will SORT the original 2D array passed to it of any dimensions.

    Private Sub sortMe(ByRef a2dArray As Array)

     

    'Put the data into a 1d array of type STRING

    Dim rowString(UBound(a2dArray, 1)) As String

    For rows As Integer = 0 To UBound(a2dArray, 1)

    For columns As Integer = 0 To UBound(a2dArray, 2)

    If columns = UBound(a2dArray, 2) Then

    rowString(rows) &= a2dArray(rows, columns).ToString

    Else

    rowString(rows) &= a2dArray(rows, columns).ToString & ","

    End If

    Next

    Next

     

     

    'Sort the rowStrings in ascending order.

    Dim swapString As String

    Dim outString As String = ""

    For check1 As Integer = 0 To UBound(rowString)

    For check2 As Integer = 0 To UBound(rowString)

    If rowString(check1) < rowString(check2) Then

    swapString = rowString(check2)

    rowString(check2) = rowString(check1)

    rowString(check1) = swapString

    End If

    Next

    Next

     

    'Generate the outString which is used for testing.>>>>

    For check1 As Integer = 0 To UBound(rowString)

    outString &= rowString(check1) & vbNewLine

    Next

    'Used for testing.>>>>

    MessageBox.Show(outString)

     

     

    'Put them back into the original 2dArray.

    'Now I'm stuck, can anyone help please?

    ' I'm tired as I was working last night. :-(

    For row As Integer = 0 To UBound(a2dArray, 1)

    For column As Integer = 0 To UBound(a2dArray, 2)

    ' ?

    Next

    Next

     

     

    End Sub

    End Class

     

     

     

    Regards,

     

    S_DS

     

    Saturday, July 28, 2007 4:14 PM