Excel Vba error: Definitions of property procedures for the same property are inconsistent

7.6k views Asked by At

It must be something obvious but I'm stuck so maybe you guys can help me.

Consider the following code inside a VBA custom object:

Private pSkipTrade As Boolean
Property Let SkipTrade(value As Double):
    If value = 0 Then
        pSkipTrade = False
    Else
        pSkipTrade = True
    End If
End Property

Public Property Get SkipTrade() As Boolean
    SkipTrade = pSkipTrade
End Property

Can anyone help me?

tks in advance!

2

There are 2 answers

6
David Zemens On BEST ANSWER

Try this:

Private pSkipTrade As Boolean
Public Property Let SkipTrade(lSkipTrade As Boolean)
    pSkipTrade = lSkipTrade
End Property
Property Get SkipTrade() As Boolean
   SkipTrade = pSkipTrade
End Property

Somewhere else in your code you are using another variable (e.g., value) to set this property. If this variable is not of Type Boolean (e.g., a Long, Double, or Decimal), then you can do something like:

Sub Test()
Dim MyObject As Object
Dim Value As Double               '## or, whatever

'# CREATE THE CLASS OBJECT
Set MyObject = New cMyObject  '## Modify to be your correct Type

Value = Application.InputBox("Enter any number")

'# ASSIGN THE PROPERTY VALUE BASED ON A LOCAL VARIABLE/LOGIC
'  use boolean logic to test whether "value = 0"
'  a non-zero value will return "True"
MyObject.SkipTrade = Not (CDbl(Value) = 0)

'# Display the value, so you can see that this is working correctly:
MsgBox "The value of SkipTrade is: " & MyObject.SkipTrade

End Sub

If you have multiple conditions, it would be better to use a Function call to return the property value, during the object's property assignment.

You can call a function like:

MyObject.SkipTrade = CheckSkipTrade(value)

With a function in your main code module like this, which could be modified to include any logic that you might need to incorporate:

Function CheckSkipTrade(value) As Boolean
Dim myVal as Boolean
    If IsNumeric(value) Then
       'returns true or false test whether 'value' = 0.
       myVal = Not(CDbl(value) = 0)
    Else:
        myVal = False

    End If

    '# Return the value of the function test:
    CheckSkipTrade = myVal
End Function

Notice that in the above example it is very explicit -- not hidden -- that you are directly making an assignment to the SkipTrade property of MyObject class object? On the contrary, your method essentially calls a subroutine hidden away in the object module, that assigns a value to the property. Does it work? Yes. Is it the best way to do it? I don't think so; it a very confusing way of coding which will make later troubleshooting (especially if you hand this project off to someone else) that much more difficult.

Coding explicitly is better than coding implicitly.

0
riderBill On

The Let parameter has to match the return type of the Get property. See this.

So you need to change the parameter in Property Let to a Boolean type:

Private pSkipTrade As Boolean
Property Let SkipTrade(value As Boolean) '<--Parameter is Boolean type
   pSkipTrade = value 
End Property

Public Property Get SkipTrade() As Boolean
    SkipTrade = pSkipTrade
End Property

Then move the logic you had in your Let property to the code that uses the property:

If price = 0 Then
    SkipTrade = False
Else
    SkipTrade = True
End If