使用没有对象限定符的属性

时间:2022-04-13 04:57:12

In Excel's VBA there are some properties which I can use without an object qualifier - for instance if I use the property ActiveCell without an object qualifier it is as if I used it on the Application object. How is it, generally, decided on which object a property is called if I don't specify an object qualifier? Can I, for instance, do something similar with my own class? Can I declare in my own class a property which can be called without an object qualifier?

在Excel的VBA中,有一些属性可以在没有对象限定符的情况下使用 - 例如,如果我使用没有对象限定符的属性ActiveCell,就好像我在Application对象上使用它一样。如果我没有指定对象限定符,通常如何决定调用属性的对象?例如,我可以和我自己的班级做类似的事情吗?我可以在我自己的类中声明一个可以在没有对象限定符的情况下调用的属性吗?

1 个解决方案

#1


2  

In VBA, a module or class is considered to inherit from the Application, then the object to which it is attached (a userform, worksheet, or ThisWorkbook), then itself. Also, if a procedure name is unambiguous, then it isn't usually necessary to qualify it when you use it.

在VBA中,模块或类被认为是从Application继承,然后是它所附加的对象(userform,worksheet或ThisWorkbook),然后是它自己。此外,如果过程名称是明确的,那么在使用它时通常不需要对其进行限定。

You can reference Application.ActiveCell as just ActiveCell, unless your class or module also contains an ActiveCell method, in which case you'd need to qualify it with Application..

您可以将Application.ActiveCell引用为ActiveCell,除非您的类或模块还包含ActiveCell方法,在这种情况下,您需要使用Application对其进行限定。

This also means that if you have MyProc as a member of MyModule, you can refer to it from another code/module file as just MyProc, but if you also have MyProc in MyOtherModule, then if you typed MyProc, it would call MyModule.MyProc if it was in MyModule, or MyOtherModule.MyProc if it was in MyOtherModule, otherwise it would raise an error stating that it was ambiguous, meaning that you'd have to qualify it to MyModule.MyProc or MyOtherModule.MyProc.

这也意味着如果你有MyProc作为MyModule的成员,你可以从另一个代码/模块文件中引用它作为MyProc,但是如果你在MyOtherModule中也有MyProc,那么如果你输入MyProc,它会调用MyModule.MyProc如果它在MyModule中,或MyOtherModule.MyProc,如果它在MyOtherModule中,否则它会引发一个错误,指出它是不明确的,这意味着你必须将它限定为MyModule.MyProc或MyOtherModule.MyProc。

However...

然而...

A class module defines the internal workings of a class, and a class must be instantiated before it can be used with New. A class's properties refer to a specific instance of a class, so must always be qualified with the instance to which it refers. I.e. if you have a class MyClass with a string property MyProperty, you would have to call MyProperty via an instance, e.g.:

类模块定义类的内部工作方式,并且必须先实例化类,然后才能将其与New一起使用。类的属性引用类的特定实例,因此必须始终使用它引用的实例进行限定。即如果你有一个带有字符串属性MyProperty的MyClass类,你必须通过一个实例调用MyProperty,例如:

Set MyClassInstance = New MyClass
Debug.Print MyClassInstance.MyProperty()

or even:

甚至:

Debug.Print (New MyClass).MyProperty()

The only way to avoid typing the name of a class instance is to declare a With block:

避免键入类实例名称的唯一方法是声明With块:

Set MyClassInstance = New MyClass
With MyClassInstance 
    Debug.Print .MyProperty()
End With

#1


2  

In VBA, a module or class is considered to inherit from the Application, then the object to which it is attached (a userform, worksheet, or ThisWorkbook), then itself. Also, if a procedure name is unambiguous, then it isn't usually necessary to qualify it when you use it.

在VBA中,模块或类被认为是从Application继承,然后是它所附加的对象(userform,worksheet或ThisWorkbook),然后是它自己。此外,如果过程名称是明确的,那么在使用它时通常不需要对其进行限定。

You can reference Application.ActiveCell as just ActiveCell, unless your class or module also contains an ActiveCell method, in which case you'd need to qualify it with Application..

您可以将Application.ActiveCell引用为ActiveCell,除非您的类或模块还包含ActiveCell方法,在这种情况下,您需要使用Application对其进行限定。

This also means that if you have MyProc as a member of MyModule, you can refer to it from another code/module file as just MyProc, but if you also have MyProc in MyOtherModule, then if you typed MyProc, it would call MyModule.MyProc if it was in MyModule, or MyOtherModule.MyProc if it was in MyOtherModule, otherwise it would raise an error stating that it was ambiguous, meaning that you'd have to qualify it to MyModule.MyProc or MyOtherModule.MyProc.

这也意味着如果你有MyProc作为MyModule的成员,你可以从另一个代码/模块文件中引用它作为MyProc,但是如果你在MyOtherModule中也有MyProc,那么如果你输入MyProc,它会调用MyModule.MyProc如果它在MyModule中,或MyOtherModule.MyProc,如果它在MyOtherModule中,否则它会引发一个错误,指出它是不明确的,这意味着你必须将它限定为MyModule.MyProc或MyOtherModule.MyProc。

However...

然而...

A class module defines the internal workings of a class, and a class must be instantiated before it can be used with New. A class's properties refer to a specific instance of a class, so must always be qualified with the instance to which it refers. I.e. if you have a class MyClass with a string property MyProperty, you would have to call MyProperty via an instance, e.g.:

类模块定义类的内部工作方式,并且必须先实例化类,然后才能将其与New一起使用。类的属性引用类的特定实例,因此必须始终使用它引用的实例进行限定。即如果你有一个带有字符串属性MyProperty的MyClass类,你必须通过一个实例调用MyProperty,例如:

Set MyClassInstance = New MyClass
Debug.Print MyClassInstance.MyProperty()

or even:

甚至:

Debug.Print (New MyClass).MyProperty()

The only way to avoid typing the name of a class instance is to declare a With block:

避免键入类实例名称的唯一方法是声明With块:

Set MyClassInstance = New MyClass
With MyClassInstance 
    Debug.Print .MyProperty()
End With