Excel Vba 错误:同一属性的属性程序定义不一致

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18446491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:58:54  来源:igfitidea点击:

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

excelvbapropertiescustom-object

提问by riderBill

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:

考虑 VBA 自定义对象中的以下代码:

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!

提前tks!

采纳答案by David Zemens

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:

您在代码的其他地方使用另一个变量(例如,value)来设置此属性。如果这个变量的类型是不是Boolean(如LongDoubleDecimal),那么你可以这样做:

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 Functioncall to return the property value, during the object's property assignment.

如果您有多个条件,最好Function在对象的属性分配期间使用调用返回属性值。

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 SkipTradeproperty of MyObjectclass 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.

请注意,在上面的示例中,您直接SkipTradeMyObject类对象的属性进行赋值是非常明确的——不是隐藏的?相反,您的方法本质上调用隐藏在对象模块中的子例程,该子例程为属性分配一个值。它有效吗?是的。这是最好的方法吗?我不这么认为;这是一种非常令人困惑的编码方式,这将使以后的故障排除(尤其是如果您将此项目交给其他人时)更加困难。

Coding explicitlyis better than coding implicitly.

显式编码比隐式编码好。

回答by riderBill

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

Let 参数必须与 Get 属性的返回类型匹配。看到这个

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

所以需要把Property Let中的参数改成布尔类型:

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:

然后将您在 Let 属性中的逻辑移动到使用该属性的代码中:

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