I have a class that has a fixed-size array of Double
, for example
例如,我有一个具有固定大小的Double数组的类
Private m_values(8) as Double
What is the correct syntax for the Let
and Get
methods for an array?
数组的Let和Get方法的正确语法是什么?
Public Property Let Values (RHS(8) as Double)
m_values = RHS
End Property
Public Property Get Values() as Double
Values = m_values
End Property
The specific parts of the syntax I am unclear about:
我不清楚语法的具体部分:
a. In the Let
method, is RHS(8) as Double
the correct way to pass an array of 8 Double
?
b. Can I copy one array to another simply using assignment? (e.g. m_values = values
)
c. For the Get
method, is it correct for the function to be declared as Double
or should it be something like as Double(8)
?
一个。在Let方法中,RHS(8)是否是传递8 Double数组的正确方法?湾我可以使用赋值将一个数组复制到另一个数组吗? (例如m_values = values)c。对于Get方法,函数声明为Double是正确的还是像Double(8)那样?
2 个解决方案
#1
4
The only way to declare a property that can hold arrays is as Variant
property.
声明可以保存数组的属性的唯一方法是Variant属性。
Private m_values As Variant
Public Property Let Values(RHS As Variant)
m_values = RHS
End Property
Public Property Get Values() As Variant
Values = m_values
End Property
Public Sub Test()
Dim x(8) As Double
x(1) = 123.55
x(2) = 456.45
x(5) = 789.66
x(8) = 123.777
' assign value to property
Values = x
' get value from property
Dim y() As Double
y = Values
Dim i As Integer
For i = 0 To UBound(y)
Debug.Print y(i)
Next
End Sub
#2
2
Try to keep the following rules:
尽量遵守以下规则:
'starting point- array with 8 elements
Dim arrStart(8) As Double
arrStart(8) = 1 'here- for testing with Local window
'passing array to another...Variant type variable
'no array declaration required
Dim arrVariant As Variant
arrVariant = arrStart
'passing array to another Double variable
'dynamic array declaration required
Dim arrEmpty() As Double
arrEmpty = arrStart
These rules work also when passing variable (as an parameter) to another property, function or subroutine. This also means that you can't declare Get Property as an array and you should declare it as a Variant type.
当将变量(作为参数)传递给另一个属性,函数或子例程时,这些规则也起作用。这也意味着您不能将Get Property声明为数组,并且应将其声明为Variant类型。
#1
4
The only way to declare a property that can hold arrays is as Variant
property.
声明可以保存数组的属性的唯一方法是Variant属性。
Private m_values As Variant
Public Property Let Values(RHS As Variant)
m_values = RHS
End Property
Public Property Get Values() As Variant
Values = m_values
End Property
Public Sub Test()
Dim x(8) As Double
x(1) = 123.55
x(2) = 456.45
x(5) = 789.66
x(8) = 123.777
' assign value to property
Values = x
' get value from property
Dim y() As Double
y = Values
Dim i As Integer
For i = 0 To UBound(y)
Debug.Print y(i)
Next
End Sub
#2
2
Try to keep the following rules:
尽量遵守以下规则:
'starting point- array with 8 elements
Dim arrStart(8) As Double
arrStart(8) = 1 'here- for testing with Local window
'passing array to another...Variant type variable
'no array declaration required
Dim arrVariant As Variant
arrVariant = arrStart
'passing array to another Double variable
'dynamic array declaration required
Dim arrEmpty() As Double
arrEmpty = arrStart
These rules work also when passing variable (as an parameter) to another property, function or subroutine. This also means that you can't declare Get Property as an array and you should declare it as a Variant type.
当将变量(作为参数)传递给另一个属性,函数或子例程时,这些规则也起作用。这也意味着您不能将Get Property声明为数组,并且应将其声明为Variant类型。