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

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

问题描述

我在 Excel 2007 中使用 vba,并且正在为类模块编写代码.

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 参数看似向后放置(与函数和子程序中的可选参数相比)之外,我很难找到有关此功能的任何文档.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.

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) 是的你的代码是可能的.

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

假设 myObject 是你的类的一个对象:

Assuming myObject is an object of your class:

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天全站免登陆