VBA 获取属性

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

VBA Get property

vba

提问by gajo357

I have a class module MyVBAclass which has a member of a custom COM class

我有一个类模块 MyVBAclass,它有一个自定义 COM 类的成员

Private mMyCOMclass As MyCOMclass
Public Property Get InnerClass() As MyCOMclass
    Set InnerClass = mMyCOMclass
End Property
Public Property Let InnerClass(value As MyCOMclass)
    Set mMyCOMclass = value
End Property
Private Sub Class_Initialize()
    Set mMyCOMclass = New MyCOMclass
End Sub

MyCOMclass has Clone() and CopyTo() methods implemented, Clone returns a new instance of MyCOMclass, and CopyTo just copies the properties to an already initialized object. This is the problem that I have, and it would be great if someone could explain how can avoid it.

MyCOMclass 实现了 Clone() 和 CopyTo() 方法,Clone 返回 MyCOMclass 的新实例,而 CopyTo 只是将属性复制到已初始化的对象。这是我遇到的问题,如果有人能解释如何避免它,那就太好了。

Dim MyVBAinstance As MyVBAclass
Set MyVBAinstance = New MyVBAclass

Dim COMinstance As MyCOMclass, copyOfCOM As MyCOMclass
Set COMinstance = New MyCOMclass
Set copyOfCOM = New MyCOMclass

' Do some stuff with COMinstance

Call COMinstance.CopyTo(copyOfCOM) ' Works fine
MyVBAinstance.InnerClass = COMinstance.Clone() ' Works fine even without Set

Call COMinstance.CopyTo(MyVBAinstance.InnerClass) ' DOESN'T WORK

回答by Peter Albert

The idea of object orientation is that an object hides it's members from the outside world. The GETand SETproperties act as a "gateway". This, no routine can simply override change the object underneath MyVBAinstance.InnerClass. What you need to do is:

面向对象的思想是一个对象向外界隐藏它的成员。在GETSET特性充当“网关”。这一点,没有例程可以简单地覆盖更改下面的对象MyVBAinstance.InnerClass。你需要做的是:

Dim objTemp as As MyCOMclass
Set objTemp = New MyCOMclass 'this will reserve the memory and store the pointer in objTemp
Call COMinstance.CopyTo(objTemp) 'this will fill the allocated memory
Set MyVBAinstance.InnerClass = objTemp 'this will copy the pointer from objTemp to MyVBAinstance.InnerClass

If you want to expose some private members of your class, you need to provide the PROPERTY GETand PROPERTY SET/PROPERTY LETmethods as you are doing. GETwill be called every time you access the property - and PROPERTY SET/PROPERTY LETis called when you assign an object/value to the property.

如果要公开类的某些私有成员,则需要在执行时提供PROPERTY GETPROPERTY SET/PROPERTY LET方法。GET将被调用每次你访问属性的时间-和PROPERTY SET/PROPERTY LET当你分配对象/值的属性被调用。

If the property is an object, you need to use PROPERTY SET- if it is of a native type (Integer, String, etc.) you must use PROPERTY LET.

In the same way, you then need to assign objects to the property with SET(e.g. Set MyVBAInstance.InnerClass = anotherObject), but native types without SET(e.g. ' MyVBAInstance.Name = "Test"`)

Thus, in your code replace LETwith SEThere:

Public Property Set InnerClass(value As MyCOMclass)
    Set mMyCOMclass = value
End Property

and then also use the SETkeyword in your code:

Set MyVBAinstance.InnerClass = COMinstance.Clone()

如果属性是对象,则需要使用PROPERTY SET- 如果它是本机类型(整数、字符串等),则必须使用PROPERTY LET.

以同样的方式,然后您需要使用SET(eg Set MyVBAInstance.InnerClass = anotherObject)将对象分配给属性,但不使用SET(eg ' MyVBAInstance.Name = "Test"`) 的本机类型

因此,在您的代码中替换LETSET

Public Property Set InnerClass(value As MyCOMclass)
    Set mMyCOMclass = value
End Property

然后也在SET你的代码中使用关键字:

Set MyVBAinstance.InnerClass = COMinstance.Clone()