2011-05-18 63 views
4

这听起来像一个愚蠢的问题,但我对拉听得出来。的Visual Basic 6阵列作为参数

我有一个Sub由此我想分析阵列并将其分配给一个类模块“对象”。

我如何去这样做。

我确实有不工作是:

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) 

我收到以下错误信息:

类型不匹配:数组或用户定义类型预期。

+0

Dabblernl的答案应该有效。如果你有更多的问题,[this](http://www.cpearson.com/excel/passingandreturningarrays.htm)似乎是相当不错的一种数组资源(它是关于VBA的,但是这足够接近于有用)。 – 2011-05-18 20:29:54

+0

@ ho1我并不反对,但他调用SetMatrixArray的语法不正确。我正在查看[this](http://msdn.microsoft.com/en-us/library/wcx04ck5(VS.85).aspx)。我错了吗? – 2011-05-18 20:38:38

+0

@Jay:不,我认为你完全正确。我太习惯VB.Net,所以直到现在我才注意到那些额外的括号。我认为你需要结合你和Dabblernl的答案。 – 2011-05-18 20:51:55

回答

6

这工作:

'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 

'类'

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

所以去除矩阵阵列的尺寸在您的类。您可以随时执行错误检查,如果尺寸必须准确9.

编辑:我删除周围的程序调用的括号不假思索测试时,它可能会影响到答案。

+1

是的,这被称为“声明动态数组”。 – Bob77 2011-05-19 03:29:55

+0

VB不允许你重新分配一个你已经给它一个大小的数组。以上声明不起作用。 – Koekiebox 2011-05-19 16:15:43

+0

让我改说一下。如果theArray()没有赋值theArray(9,9),它会工作。我修改了答案。做得好。今天学到了新东西! – Koekiebox 2011-05-20 13:49:10

3

我认为你需要通过数组作为一个变型多维数组

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

退房this文章。

3

当你调用customObj.SetMatrixArray()尝试之一:

删除周围的过程参数的括号:

customObj.SetMatrixArray theArray 

- 或 -

Call你作序电话:

Call customObj.SetMatrixArray(theArray) 
+0

+1您可以在VB6中遇到奇怪的情况,其中添加额外的括号将您的参数转换为表达式 – MarkJ 2011-05-19 08:42:07