Let/Get属性中的可选参数如何工作? [英] How do optional Parameters in Let/Get Properties work?

查看:70
本文介绍了Let/Get属性中的可选参数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将vba与Excel 2007一起使用,并正在为类模块编写代码.

I am using vba with Excel 2007, and am writing code for a class module.

1)以下代码是否可能?...
本质上,我有两个枚举,分别称为eDATASETeDATATSUBSET. eDATASET中的特定值应触发Let属性中可选传递的参数的赋值.像这样:

1) Is the following code even possible?...
Essentially I have two enums, call them eDATASET and eDATATSUBSET. A particular value from eDATASET should trigger an assignment from the optionally passed parameter in a Let property. Something like this:

Public Property Let foo(Optional ByVal lngSubSet as eDATASUBSET, _  
                        ByVal lngSuperSet as eDATASET)
    Select Case lngSuperSet
        Case eDATASET.abc, eDATASET.def
            mlngBar = lngSuperSet
        Case eDATASET.xyz
            '// if lngSubSet not passed, trigger error code...
            mlngBar = lngSubSet
    End Select
End Property

2)调用对象时,如何甚至将可选参数传递给可写属性...
除了Optional参数的表象向后放置(与函数和subs中的可选参数相比)之外,我很难找到有关此功能的任何文档. VBA帮助说:

2) How do I even pass an optional parameter to a writable property when calling the object...
Aside from the seemingly backwards placement of the Optional parameters (compared with optional parameters in functions and subs), I am having trouble finding any documentation on this feature. The vba help says this:

可选.指示不需要参数.如果使用,则arglist中的所有后续参数也必须是可选的,并使用Optional关键字声明.请注意,Property Let表达式的右侧不可能是可选的.

Optional. Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Note that it is not possible for the right side of a Property Let expression to be Optional.

以及 vbusers.com 中的以下内容.两者都没有解释使用的方式.因此,当从代码模块调用对象时,我将如何传递可选参数... oObj.foo = ???

and the following from vbusers.com. Neither explain much in the way of usage. So how would i pass the optional parameter when calling the object from a code module... oObj.foo = ???

3)有更好的方法吗?...
我对oop有基本的了解(至少在vba中是如何实现的).有没有更好的方法来有条件地将参数接受到对象中?

3) Is there a better way to do this?...
I have a basic understanding of oop (at least in how it is implemented in vba). Is there a better way to conditionally accept a parameter into an object?

推荐答案

1)是,您的代码是可能的.

1) Yes your code is possible.

2)这是您传递参数的方式:

假设myObject是您的类的对象:

myObject.foo(lngSubSet) = lngSuperSet 

arglist 中的参数位置确实看起来很奇怪,但这就是您的VBA.假设您有4个参数,其中两个是可选参数,加上右手边.您将这样放置它们:

The placement of arguments in the arglist does indeed look weird, but that's VBA for you. Say you have 4 arguments, two of which are optional, plus your right hand side. You would place them like this:

Public Property Let foo(arg1, arg2, Optional arg3, Optional arg4, _  
                        RHS)

并像这样使用它们(假设您选择退出arg4):

and use them like this (assuming you're opting out of arg4):

myObject.foo(arg1,arg2,arg3) = RHS

3)有更好的方法吗?总是存在,具体取决于您问的是谁.您可以将lngSubSet参数完全作为一个单独的属性使用.这就是我倾向于这样做的方式.但就您而言,您的处事方式可能会对您有效.我不知道,这主要是口味问题,取决于您的特定应用.

3) Is there a better way to do this? There always is, depending who you ask. You could have your lngSubSet argument as a separate property entirely. That's how I tend to do it. But in your case, your way of doing things may work well for you. I don't know, it's largely a question of taste and dependent on your specific application.

这篇关于Let/Get属性中的可选参数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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