为什么要在VBA类模块中声明新类型? [英] Why declare a New Type within a VBA Class Module?

查看:378
本文介绍了为什么要在VBA类模块中声明新类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遵循

I want to follow the MVP Userform architecture described in this excellent answer by Mathieu Guindon. However, I don't understand why in the FilterModel Class Module, and also the FilterForm Userform module, new Types are created within the Class whose elements are then redeclared as Properties of the Class. Why is this necessary? Why not just declare SelectedFilter as a property of type string? Am I missing something?

推荐答案

大多数属性都需要一个后备字段.通常看起来像这样:

Most properties need a backing field. Typically it would look like this:

Option Explicit
Private mFoo As String

Public Property Get Foo() As String
    Foo = mFoo
End Property

Public Property Let Foo(ByVal value As String)
    mFoo = value
End Property

locals 工具窗口中查看此类的实例时,您将同时看到mFoo私有后备字段和公共Foo属性-并且该类具有更多内容时而不是少数成员,它很快就会变得嘈杂.

When you look at an instance of this class in the locals toolwindow, you'll see both the mFoo private backing field and the public Foo property - and when the class has more than a handful of members, it quickly gets noisy.

此外,我真的不喜欢这种m前缀方案,但是由于VBA不区分大小写,因此对于Foo属性,您不能具有foo备用字段.

Plus I really don't like this m prefixing scheme, but since VBA is case-insensitive, you can't have a foo backing field for a Foo property.

通过声明Private Type来保持封装的实例状态...

By declaring a Private Type to hold the encapsulated instance state...

Private Type InternalState
    Foo As String
End Type
Private this As InternalState

Public Property Get Foo() As String
    Foo = this.Foo
End Property

Public Property Let Foo(ByVal value As String)
    this.Foo = value
End Property

...现在,属性及其支持字段可以使用相同的标识符,不需要任何前缀,我们会为this的所有成员获得一个自动完成列表,而locals工具窗口现在正在将实例状态值分组在this下,是该类中唯一的私有字段,这使调试工具更加简洁.

...now the property and its backing field can use the same identifier, there's no need for any prefix, we get an autocompletion list for all members of this, and the locals toolwindow is now grouping the instance state values under this, the only private field in the class, which makes the debugging tool much cleaner.

这不是必要,但我找不到不这样做的充分理由.确实,这比最佳实践更重要的是样式/偏好,但它有明显的好处.

It's not a necessity, but I can't find a good reason not to do it. It's really a style/preference thing more than a best practice, but it has clear benefits.

为什么不仅仅将SelectedFilter声明为字符串类型的属性?

Why not just declare SelectedFilter as a property of type string?

如果那是说为什么不只声明一个字符串类型的公共 field ",那么这是一个不同的问题,答案是不同的.

If that meant to say "why not just declare a public field of type string", then it's a different question, with a different answer.

每个类都定义一个默认接口,其成员是该类的Public成员.但是接口不会公开字段,因此,如果您在类上有一个公共字段,则其默认接口将具有Property GetProperty Let/Set访问器:通过定义显式的Property成员而不是仅一个公共字段,您可以使类定义与其默认接口一致. ..并且您正在封装内部状态-封装是OOP的四大支柱之一:没有业务公开的事物不应该公开.使用属性,您可以控制如何分配支持私有字段:类外部的任何人都不能访问它.例如,如果外部代码试图将Foo分配给空字符串或与某些正则表达式模式不匹配的字符串,则可能需要运行一些验证逻辑并引发错误.

Every class defines a default interface, whose members are the class' Public members. But interfaces don't expose fields, so if you have a public field on a class, its default interface will have Property Get and Property Let/Set accessors for it: by defining explicit Property members instead of just a public field, you are making the class definition consistent with its default interface. ..and you're encapsulating the internal state - encapsulation being one of the 4 pillars of OOP: things that have no business being public, shouldn't be public. With a property, you get to control how the backing private field is assigned: no one outside the class can access it. Maybe you need to run some validation logic and raise an error if external code attempts to assign Foo to an empty string, or to a string that doesn't match some regex pattern, for example.

使用属性与公共字段的优点超出了此问题的范围(并且已经在其他地方解答),但是该主题与语言无关,因此不必将您的研究限于VBA: VBA中使用(或不使用)属性与公共字段的关系与Java或C#中的属性相同.

The advantages of using properties vs public fields is beyond the scope of this question (and already answered elsewhere) though, but the topic is very much language-agnostic, so don't necessarily confine your research to VBA: the reasons to use (or not) properties vs public fields are the same in VBA as they are in Java or C#.

这篇关于为什么要在VBA类模块中声明新类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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