Koekiebox
Koekiebox

Reputation: 5963

Visual Basic 6 Array as Argument

This might sound like a stupid question, but I am about to pull hear out.

I have a Sub whereby I want to parse an array and assign it to a Class Module "Object".

How do I go about doing this.

What I do have that isn't working is:

Private matrix(9,9) As Integer
'The Setter Sub
Public Sub SetMatrixArray(arrValToSet() as Integer)
    matrix = arrValToSet
End Sub


'In the caller module / class module I have the following code to parse the array.

Dim theArray(9,9) As Integer
Dim customObj as CustomObject
customObj.SetMatrixArray(theArray)

I get the following error message:

Type mismatch: array or user-defined type expected.

Upvotes: 4

Views: 5509

Answers (3)

Dabblernl
Dabblernl

Reputation: 16101

This works:

 'In the caller module / class module I have the following code to parse the array.'
    Dim theArray(9,9) As Integer 
    Dim customObj as CustomObject 
    customObj.SetMatrixArray theArray

'The Class'

Private matrix() As Integer 
       'The Setter Sub '
       Public Sub SetMatrixArray(arrValToSet() as Integer)
       matrix = arrValToSet
    End Sub 

So remove the dimensioning of the matrix array in your class. You can always implement errorchecking if the dimensions must be exactly 9.

EDIT: I removed the parens around the procedure calling without thinking while testing, it may influence the answer.

Upvotes: 6

Jay Riggs
Jay Riggs

Reputation: 53593

When you call customObj.SetMatrixArray() try either:

Dropping the parens around the procedure parameter:

customObj.SetMatrixArray theArray

-- or --

Prefacing your call with Call:

Call customObj.SetMatrixArray(theArray)

Upvotes: 3

Phil Murray
Phil Murray

Reputation: 6554

I think you need to pass the array as a variant for multidimensional arrays

Public Sub SetMatrixArray(arrValToSet as Variant)
    matrix = arrValToSet
End Sub

Check out this article.

Upvotes: 3

Related Questions