Passing Arrays to Functions

Passing an array as an element of a 2D Array to a Function that...

Page 1 of 1

3 Replies - 5537 Views - Last Post: 18 April 2010 - 11:23 AM Rate Topic: -----

#1 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Passing Arrays to Functions

Posted 18 April 2010 - 10:41 AM

I'm trying to pass an array from a 2D Array to a function that takes a 1D array, but each time I do I get the error Number of indices is less than the number of dimensions of the indexed array.. I know using a structure would solve the problem, but unfortunately the assignment is to use a 2D Array instead of a structure. I've declared the 2D array as such:
Const MAX_ARR_SIZE = 63
Public HouseholdCollection(MAX_ARR_SIZE, 2) As UInteger
Const IDENTIFICATION_CODE = 0
Const MEMBER_COUNT = 1
Const INCOME = 2

Dim CurrentRecord As UInteger = 0


One of the functions/subroutines I'm using with the array is this:
Sub Delete(ByVal Household() As UInteger)
    Household(IDENTIFICATION_CODE) = 0
    Household(MEMBER_COUNT) = 0
    Household(INCOME) = 0
End Sub


The function/subroutine itself never has any problems, but its implementation raises the error. I implement it like this:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnDelete.Click
    If CurrentRecord > 0 Then
        CurrentRecord -= 1
    End If
    LoadRecord(HouseholdCollection(CurrentRecord)) 'Another function very similar to Delete()'
    Delete(HouseholdCollection(CurrentRecord + 1))
End Sub


I've done all I can think of, including declaring the arguments as ParamArrays, but nothing worked.


Also, when using a For Each loop, I get Value of type 'UInteger' cannot be converted to '1-dimensional array of UInteger'.. I can use just a normal For loop, but I was curious as to why that would happen as well.
Dim IncomeSum As ULong
Dim ImpoverishedSum As UShort
For Each Item As UInteger() In HouseholdCollection
    If Item(IDENTIFICATION_CODE) = 0 Then
        Continue For
    End If
    RealRecordCount += 1
    IncomeSum += Item(INCOME)
    If IsImpoverished(Item) Then
        ImpoverishedSum += 1
    End If
Next


Is This A Good Question/Topic? 0
  • +

Replies To: Passing Arrays to Functions

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Passing Arrays to Functions

Posted 18 April 2010 - 10:57 AM

use ByRef not ByVal. but if your array is public you can use it in your function without declaration. if you use ByVal the function create a new array every time you call it.

This post has been edited by NoBrain: 18 April 2010 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 Vermiculus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Re: Passing Arrays to Functions

Posted 18 April 2010 - 11:03 AM

Ah that would have been a dumb mistake, and definitely would have come up. But the errors still remain. :(
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Passing Arrays to Functions

Posted 18 April 2010 - 11:23 AM

hmm the only way i think of is using ArrayList and make array of array and pass them to the function like so
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim arr1(10) As Integer
        Dim ListOfArray As New ArrayList
        ListOfArray.Add(arr1)
        ListOfArray.Add(arr1)
        For i As Int16 = 0 To ListOfArray.Count - 1
            ArrayTest(ListOfArray.Item(i))
        Next

    End Sub
    Private Sub ArrayTest(ByVal myNEWArray() As Integer)
        Debug.Print(myNEWArray(0))
    End Sub
End Class



or to make a new one dimension array and fill only the dimension of your original one and pass it byref to the function

This post has been edited by NoBrain: 18 April 2010 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1