VBA复杂的Getter,Setter语法

时间:2015-03-25 13:30:56

标签: vba excel-vba excel

您好我是VBA的新手我需要创建一个具有相对复杂的Getter和Setter的对象。为了做到这一点,我不断检查MSDN,但很明显我不理解,因为VBE一直在突出显示行开始和结束:属性(它显然需要Get或Let ??),Get(显然需要标识符),让(它显然也需要标识符。)

但我正在尝试遵循更简洁的表示法,其中Get和Let方法位于Microsoft在其示例中使用的Property Statement中(参见上面的链接)。

有人能告诉我我的语法错误(或微软的文档)???

谢谢

Private Matrix() As Vector
Property Transition()
    Public Get(Old_S As String, New_S As String, Period As Integer) As Double
        ' Some Code
        Return Matrix(Column, Row).Value(Period)
    End Get
    Public Let(Old_S As String, New_S As String, Vector_String As String)
        ' Some Code
        Matrix(Row, Column).Value = Vector_String
    End Let
End Property

3 个答案:

答案 0 :(得分:8)

您正在阅读VB.NET的文档。这就是你感到困惑的原因。 VBA中的属性语法不同。在VBA中,属性的GetLet未组合在一起。它们需要单独列出,基本上类似于两种不同的方法:

Private mMyProperty As String

Public Property Get MyProperty() As String
    MyProperty = mMyProperty
End Property

Public Property Let Transition(Value As String)
    mMyProperty = Value
End Property

对于VBA参考资料,请尝试启动here

答案 1 :(得分:2)

您的问题是您正在阅读.Net帮助文件! :)

假设您有一个有效的Vector类,您的属性需要像这样定义:

Private Matrix()              As Vector
Public Property Get Transition(Old_S As String, New_S As String, Period As Integer) As String
' Some Code
    Transition = Matrix(Column, Row).Value(Period)
End Property
Public Property Let Transition(Old_S As String, New_S As String, Period As Integer, Vector_String As String)
' Some Code
    Matrix(Row, Column).Value = Vector_String
End Property

请注意,两个过程的参数列表必须匹配,除非Letter具有与Getter返回的类型相同的其他参数。

拥有一个接受VBA参数的Getter属性也有点不寻常 - 通常可以作为方法实现。

答案 2 :(得分:1)

看起来您在VBA中使用VB.Net语法。这不起作用,因为它们是完全不同的语言。以下是VBA Property Keyword的正确文档的链接。

以下是如何在VBA中编写它。

Private Matrix() As Vector
Public Property Get MatrixValue(Old_S As String, New_S As String, Period As Integer) As Double
    ' Some Code
    MatrixValue = Matrix(Column, Row).Value(Period)
End Property

Public Property Let MatrixValue(Old_S As String, New_S As String, Vector_String As String)
    ' Some Code
    Matrix(Row, Column).Value = Vector_String
End Property

但是你会遇到Get财产的问题。您可以 pass parameters into Get,但它并不完全直观。我认为你真正想要的是一个功能。

Public Function GetMatrixValue(Old_S As String, New_S As String, Period As Integer) As Double
    ' Some Code
    MatrixValue = Matrix(Column, Row).Value(Period)
End Function