Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Complicated Getter, Setter syntax

Tags:

excel

vba

Hi I'm rather new to VBA I need to create an object with relativelly complicated Getter and Setter. To do this I am constantly checking with MSDN but clearly I am not understanding something because VBE keep highlighting lines starting and closing: Property (it appaently needs Get or Let??), Get(it apparently needs identifier), Let(it apparently needs identifier as well).

But I am trying to follow more concise notation where Get and Let methods are within a Property Statement which is used by Microsoft in its examples(see link above).

Can someone tell me where is my syntax wrong(or Microsoft's Documentation for that matter)???

Thank you

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
like image 357
sgp667 Avatar asked May 10 '26 09:05

sgp667


2 Answers

You are reading the documentation for VB.NET. That's why you are confused. The syntax for properties in VBA is different. In VBA, the Get and Let for a property are not grouped together. They need to be listed separately, essentially like two separate methods:

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

For VBA reference material, try starting here.

like image 157
Steven Doggart Avatar answered May 14 '26 16:05

Steven Doggart


Your issue is that you are reading .Net help files! :)

Assuming you have a valid Vector class, your properties need to bde defined like this:

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

Note that the argument list for both procedures must match except that the Letter has an additional argument of the same type that the Getter returns.

It is also slightly unusual to have a Getter property that accepts arguments in VBA - that would more usually be implemented as a method.

like image 24
Rory Avatar answered May 14 '26 17:05

Rory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!