Visual Basic 6.0:函数返回数组

时间:2022-10-13 16:36:22

Is there better way how to create function returning array than:

是否有更好的方法来创建函数返回数组:

function foo
 Dim bar(1 to 2)as double
 bar(1)=1
 bar(2)=2
 foo=bar
end function

and in code:

并在代码中:

arrayx=foo

Because when I declare Dim arrayx(1 to 2) as double it throws an error "can't assign array" When I don't declare the variable arrayx it doesn't seem to have any problems.

因为当我将Dim arrayx(1到2)声明为double时,它会抛出错误“无法分配数组”当我没有声明变量arrayx时,它似乎没有任何问题。

2 个解决方案

#1


13  

As Matt suggests, this error:

正如马特所说,这个错误:

Compile error: Can't assign to array

编译错误:无法分配给数组

stems from the fact that you've tried to assign the return value of Foo() to a fixed array, rather than a dynamic array. You simply need to indicate to the compiler that the variable you're declaring is an array, not the actual size of the array. It will figure out the size based on the size of the array that is returned.

源于你试图将Foo()的返回值赋给固定数组而不是动态数组的事实。您只需要向编译器指出您声明的变量是数组,而不是数组的实际大小。它将根据返回的数组大小计算出大小。

Additionally, you should always specify a return value type for your functions. You do that in VB by placing an As Type clause at the end of the function declaration. In this case, you want an array of doubles, written as Double().

此外,您应始终为函数指定返回值类型。您可以在VB中通过在函数声明的末尾放置一个As Type子句来实现。在这种情况下,您需要一个双精度数组,写为Double()。

So, rewrite your code to look like this, incorporating both of those changes:

因此,重写您的代码看起来像这样,并结合这两个变化:

Function Foo() As Double()      ' note the specification of a return value
   Dim bar(1 To 2) As Double
   bar(1) = 1
   bar(2) = 2
   Foo = bar
End Function

Private Sub Command1_Click()
   Dim arrayx() As Double       ' note the declaration of a DYNAMIC array
   arrayx = Foo()
   MsgBox arrayx(1)
End Sub

This code displays a message box with the value "1", just as expected.

此代码显示一个值为“1”的消息框,正如预期的那样。

#2


2  

You can only assign to a dynamic array. Try:

您只能分配给动态数组。尝试:

Dim arrayx() as Double

#1


13  

As Matt suggests, this error:

正如马特所说,这个错误:

Compile error: Can't assign to array

编译错误:无法分配给数组

stems from the fact that you've tried to assign the return value of Foo() to a fixed array, rather than a dynamic array. You simply need to indicate to the compiler that the variable you're declaring is an array, not the actual size of the array. It will figure out the size based on the size of the array that is returned.

源于你试图将Foo()的返回值赋给固定数组而不是动态数组的事实。您只需要向编译器指出您声明的变量是数组,而不是数组的实际大小。它将根据返回的数组大小计算出大小。

Additionally, you should always specify a return value type for your functions. You do that in VB by placing an As Type clause at the end of the function declaration. In this case, you want an array of doubles, written as Double().

此外,您应始终为函数指定返回值类型。您可以在VB中通过在函数声明的末尾放置一个As Type子句来实现。在这种情况下,您需要一个双精度数组,写为Double()。

So, rewrite your code to look like this, incorporating both of those changes:

因此,重写您的代码看起来像这样,并结合这两个变化:

Function Foo() As Double()      ' note the specification of a return value
   Dim bar(1 To 2) As Double
   bar(1) = 1
   bar(2) = 2
   Foo = bar
End Function

Private Sub Command1_Click()
   Dim arrayx() As Double       ' note the declaration of a DYNAMIC array
   arrayx = Foo()
   MsgBox arrayx(1)
End Sub

This code displays a message box with the value "1", just as expected.

此代码显示一个值为“1”的消息框,正如预期的那样。

#2


2  

You can only assign to a dynamic array. Try:

您只能分配给动态数组。尝试:

Dim arrayx() as Double