在 VBA 中使用属性集时获取“属性的无效使用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17515827/
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:05:03  来源:igfitidea点击:

Getting 'Invalid use of property' when using Property Set in VBA

excelvbaproperties

提问by meteorainer

I know there are tons of threads and questions about this and it's pretty obvious, generally, where the error is. Most folks aren't using the SET keyword when moving objects around. I am.

我知道有很多关于这个的话题和问题,而且很明显,一般来说,错误在哪里。大多数人在移动对象时不使用 SET 关键字。我是。

Here's what's happening:

这是发生了什么:

This is in an excel sheet so I've made a little set of functions to keep track of the columns and make an index so that each time the app runs it will reindex the columns so I can do things like .Cells(row_num, pCust_index("custID"))in case of column changes.

这是在一个 excel 表中,所以我做了一些函数来跟踪列并创建索引,这样每次应用程序运行时它都会重新索引列,这样我就可以做一些事情,比如.Cells(row_num, pCust_index("custID"))在列更改的情况下。

I have a form called custContagions. It's just a little modal window that allows users to add/remove/edit customer's contagious status. It contains a property:

我有一个名为 custContagions 的表单。它只是一个小的模态窗口,允许用户添加/删除/编辑客户的传染状态。它包含一个属性:

Private pCust_index as dictionary

It also contains this property setter:

它还包含此属性设置器:

Public Property Set set_cust_index(ByRef custIndex As Dictionary)
    Set pCust_index = New Dictionary
    Set pcust_index = custIndex
End Property

Pretty straight forward right? Takes a dictionary object, resets my index and points to the existing passed object.

很直接吧?获取一个字典对象,重置我的索引并指向现有的传递对象。

Now, on the calling form I have the other side:

现在,在调用表上,我有另一边:

Private Sub newCustContagious_LBL_Click()
    Dim contForm as New custContagions
        Call contForm.set_cust_index(pCust_index)  'note pCust_index is a private here too
        Call contForm.Show
...

I'm getting the Invalid Use of Property compiler error on the set_cust_index call.

我在 set_cust_index 调用中收到 Invalid Use of Property 编译器错误。

What did I miss?

我错过了什么?

回答by GSerg

Most folks aren't using the SET keyword when moving objects around

大多数人在移动对象时不使用 SET 关键字

Then they are not moving objects around. The Setkeyword is the way to move object around.
There is also CopyMemoryto directly copy the ObjPtr, but I do not believe most folks do that.

然后他们不会四处移动物体。该Set关键字是移动物体周围的道路。
也有CopyMemory直接复制的ObjPtr,但我不相信大多数人会这样做。

Pretty straight forward right?

很直接吧?

Not quite. You create a dictionary, immediately discard it and replace with another dictionary passed as a parameter. You should remove the first of the two lines, and make the param ByVal:

不完全的。您创建一个字典,立即丢弃它并替换为另一个作为参数传递的字典。您应该删除两行中的第一行,并制作 param ByVal

Public Property Set set_cust_index(ByVal custIndex As Dictionary)
    Set pcust_index = custIndex
End Property

I'm getting the Invalid Use of Property compiler error

我收到 Invalid Use of Property 编译器错误

You declared a property and then use it as a sub. With a property, you should have done:

您声明了一个属性,然后将其用作子项。有了财产,你应该做到:

Set contForm.set_cust_index = pCust_index

At which point the set_cust_indexname does not look great. It would make a sensible name for a sub (Public Sub set_cust_index(ByVal custIndex As Dictionary)), but for a property you would be better off with Public Property Set cust_index(ByVal custIndex As Dictionary).

在这一点上,这个set_cust_index名字看起来不太好。它将为 sub ( Public Sub set_cust_index(ByVal custIndex As Dictionary))取一个合理的名称,但对于属性,您最好使用Public Property Set cust_index(ByVal custIndex As Dictionary).