将类设置为PublicNotCreatable的潜在后果是什么? [英] What are the potential consequences of setting a class to PublicNotCreatable?

查看:278
本文介绍了将类设置为PublicNotCreatable的潜在后果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个具有以下定义的类TestClass:

Say I have a class TestClass with the following definition:

Option Explicit

Public x As Variant

我希望在工作簿打开时创建此类的实例,并在其他工作簿事件中使用.因此,在ThisWorkbook模块中,我添加:

I want an instance of this class to be created when the workbook opens, and usable in other workbook events. So in the ThisWorkbook module, I add:

Public tc As TestClass

Private Sub Workbook_Open()

Set tc = New TestClass
tc.x = "abc"

End Sub

编译时,出现错误:

Private object modules cannot be used in public object modules as 
parameters or returns types for public procedures, as public data 
members, or as fields of public user defined types

我可以通过转到TestClass的属性"窗口并将Instancing设置为PublicNotCreatable来解决此问题.似乎这并没有影响类的行为,也没有影响我在此VBA项目的各种函数中创建类的本地实例的能力.更改此实例设置是否存在任何潜在问题?

I was able to resolve this by going to the Properties Window for TestClass and setting Instancing to PublicNotCreatable. It seems that this hasn't affected the behavior of the class or my ability to create local instances of it within various functions in this VBA project. Does changing this Instancing setting have any potential problems?

推荐答案

PublicNotCreatable是,因此您可以在其他VBProject中使用该类. NotCreatable部分意味着,无论托管VBProject的类是什么,都必须创建任何新实例并将其传递给另一个VBProject.另一个VBProject不能自己创建它们.

PublicNotCreatable is so you can use that class in a different VBProject. The NotCreatable part means that whatever VBProject hosts the class has to create any new instances and pass those to the other VBProject. The other VBProject cannot create them by itself.

您想要做的是在标准模块中的Public tc As TestClass,并使您的类实例化为Private.在标准模块中声明一个公共变量将使该变量在项目中的任何地方都可用.

What you want to do is Public tc As TestClass in a standard module and keep your class instancing Private. Declaring a variable public in a standard module makes that variable available everywhere in your project.

您所做的是创建Workbook对象的ThisWorkbook实例的自定义属性. ThisWorkbook是一个类模块(工作表模块和用户窗体也是如此).这些类模块是特殊的,因为它们具有用户界面组件(工作簿,工作表和表单),但是它们都是类模块.您可以像在自定义类中一样在ThisWorkbook中定义属性和方法.那就是你所做的.您创建了一个名为tc的自定义属性.

What you did is created a custom property of the ThisWorkbook instance of the Workbook object. ThisWorkbook is a class module (so are the sheet modules and userforms). Those class modules are special because they have a user interface component (workbooks, worksheets, and forms), but they're class modules all the same. You can define properties and methods in ThisWorkbook just like you do in a custom class. And that's what you did. You created a custom property called tc.

要在类中创建属性,可以使用Property Get/Let或Public.使用Public的快捷方式看起来很像在标准模块中声明一个public变量,但这并不完全相同.

To create a property in a class, you can either use Property Get/Let or Public. The shortcut of using Public looks a lot like declaring a public variable in a standard module, but it's not quite the same.

那为什么会出错呢?如果您的自定义类实例是另一个类的属性,则VBA不允许您创建该自定义类的实例,因为您最终可能会得到一个子类而没有父类.

So why the error? When your custom class instance is a property of another class, VBA won't let you create an instance of the custom class because you could end up with a child class and no parent class.

因此,请保留您的实例私有性,并在标准模块中声明您的公共变量.我有一个名为MGlobals的特殊模块,其中保存了所有公共变量.如果我有一对以上,则可能是我做错了.

So keep your instancing private and declare your public variables in a standard module. I have a special module called MGlobals where I keep all my public variables. If I have more than a couple, I'm probably doing something wrong.

这篇关于将类设置为PublicNotCreatable的潜在后果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆