VBA类方法链接 [英] VBA Class Method Chaining

查看:72
本文介绍了VBA类方法链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种链类方法"的方法,例如Range对象可以做"Range.Borders.Color"之类的事情,我想Borders部分是它自己的类,正由范围类,但我不知道如何实现与自己的类相似的东西-我什至不知道这叫什么,经过数小时的搜索,我认为我可能会慢慢地学习VBA.

I'm looking for a way to 'chain class methods', for example the Range object can do things like "Range.Borders.Color", I guess that the Borders part is it's own class which is being accessed by the Range class but I have no idea how to implement something similar with my own classes - Nor do I even know what this is called and after hours of searching I think I might slowly be un-learning VBA.

任何人都可以a)提供我可以复制的代码,或者b)告诉我这叫什么,甚至可以在一个有用的方向上推我吗?

Can anybody either a) Provide code which I could look at to replicate or b) Tell me what this is called and maybe even nudge me in a helpful direction?

由于我知道在不提供任何代码的情况下询问代码,这使我看起来像个鸡巴,请考虑以下伪代码.我知道这太恐怖了,但这可能对我有帮助:

As I know asking for code without providing any makes me look like a dick, consider the following pseudo-code. I know it's horrific but it might help me make any sense:

主要---------------------------------------------- --------------------------

main ------------------------------------------------------------------------

Dim obj as class1
set obj = new class1

obj.Target = Range("A1:B5")
obj.Borders.Add

'A1:B5 put into modRange then given borders

class1 ---------------------------------------------- --------------------------

class1 ------------------------------------------------------------------------

Private modRange as range

Public Property Let Target(newTarget as Range)
  set modRange = newTarget
End Property

Public Property Borders()
    Public Sub Add()
        'Code to add borders to modRange
    End Sub 
    Public Sub Remove()
        'Code to remove borders from modRange
    End Sub 
End Property

我知道这不是实际代码的样子.但由于我不知道语法,所以这是我能想到的最接近的东西.我想真正的事情是将class1链接到其他类模块.也许.

I know this is not how the actual code would look. but as I don't know the syntax this is the closest thing I can imagine. I guess the real thing would have class1 linking to other class modules. Maybe.

作为旁注.如果我确实有一个名为边界"的类(我可能不会),作为该类1对象的一部分,它是否会与Range对象的Borders部分冲突,并且名称相似?还是私有范围可以节省您的一天?

As a side note. If I did have a class called "Borders" (I probably wont) as part of this class 1 object, would it conflict with the Borders portion of the Range object as well as it has a similar name? Or will the Private scope save the day?

(.Borders.Add/Remove作为一个我知道的类有点荒谬,我只是在语法之后才诚实-诚实)

(The .Borders.Add/Remove is a bit ridiculous to have as a class I know, I'm really only after the syntax - Honest)

推荐答案

要具有对象的复杂属性,您需要创建一个新类,然后在父类中创建该类的实例.因此,如果要使用Class1.Borders.Add()之类的东西,则首先必须创建一个新的CBorders类(我曾经在VB6/VBA中将C放在我的类名之前,以避免名称冲突).像这样:

To have complex properties of an object, you need to create a new class and then create an instance of that class in the parent class. So if you want to have something like Class1.Borders.Add(), you'd first have to create a new CBorders class (I used to prepend C to my class names in VB6 / VBA to avoid name collisions). Something like:

'- in class CBorder
Private m_lColor As Long

Public Property Get Color() As Long
    Color = m_lColor
End Property

Public Property Let Color(ByVal lNewColor As Long)
    m_lColor = lNewColor
End Property

Public Sub Reset()
    m_lColor = 0
End Sub

...

然后在Class1内,您将得到以下内容:

Then inside Class1, you'd have something like this:

Private m_oBorder As CBorder

Private Sub Class_Initialize()
    ...
    Set m_oBorder = New CBorder
    ...
End Sub

Public Property Get Border() As CBorder
    Set Border = m_oBorder
End Property

...

然后您可以执行以下操作:

Then you can do this:

Dim obj As Class1
Set obj = New Class1

obj.Borders.Color = ...
...

请注意如何作为obj实例的成员访问Class1Borders属性,然后如何使用CBorder类的Color属性.通过将这些值创建为属性,可以将这些调用链接在一起.

Notice how the Borders property of Class1 is accessed as a member of the obj instance and then how the Color property of the CBorder class is used. Creating these values as properties is what lets you chain these calls together.

您还需要错误检查和验证代码-为了使示例简短,我省略了这些代码.

You'd need error checking and validation code as well - I left those out to keep the example short.

这篇关于VBA类方法链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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