vba Let/Get 属性中的可选参数如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6642142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do optional Parameters in Let/Get Properties work?
提问by Hari Seldon
I am using vba with Excel 2007, and am writing code for a class module.
我在 Excel 2007 中使用 vba,并且正在为类模块编写代码。
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:
1)下面的代码甚至可能吗?...
基本上我有两个枚举,调用它们eDATASET
和eDATATSUBSET
. 特定值 fromeDATASET
应触发Let
属性中可选传递的参数的赋值。像这样的东西:
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) 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:
2)我如何在调用对象时将可选参数传递给可写属性......
除了Optional
参数的看似向后放置(与函数和子程序中的可选参数相比),我无法找到任何关于此的文档特征。vba 帮助是这样说的:
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.
可选的。表示不需要参数。如果使用,则 arglist 中的所有后续参数也必须是可选的,并使用 Optional 关键字声明。请注意,Property Let 表达式的右侧不可能是 Optional。
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 = ???
以及以下来自vbusers.com 的内容。在使用方式上也没有解释太多。那么从代码模块调用对象时我将如何传递可选参数...oObj.foo = ???
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?
3)有没有更好的方法来做到这一点?...
我对oop有一个基本的了解(至少在vba中它是如何实现的)。有没有更好的方法来有条件地将参数接受到对象中?
回答by Jean-Fran?ois Corbett
1) Yesyour code is possible.
1)是的,您的代码是可能的。
2) This is how you pass an argument:
2)这是你传递参数的方式:
Assuming myObject
is an object of your class:
假设myObject
是您班级的一个对象:
myObject.foo(lngSubSet) = lngSuperSet
The placement of arguments in the arglistdoes 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:
arglist中参数的位置确实看起来很奇怪,但这对您来说是 VBA。假设你有 4 个参数,其中两个是可选的,加上你的右手边。你可以这样放置它们:
Public Property Let foo(arg1, arg2, Optional arg3, Optional arg4, _
RHS)
and use them like this (assuming you're opting out of arg4
):
并像这样使用它们(假设您选择退出arg4
):
myObject.foo(arg1,arg2,arg3) = RHS
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.
3)有没有更好的方法来做到这一点?总是有,这取决于你问谁。你可以把你的lngSubSet
论点完全作为一个单独的财产。这就是我倾向于这样做的方式。但就你而言,你的做事方式可能对你很有效。我不知道,这主要是品味问题,取决于您的具体应用。
回答by david
This question is marked as the answer to a completely different question, "Let property of VBA class modules - is it possible to have multiple arguments? [duplicate]", so it's possible that people wanting an answer to that question might follow the link and wind up here.
这个问题被标记为一个完全不同的问题的答案,“让 VBA 类模块的属性 - 是否可以有多个参数?[重复]”,因此想要回答该问题的人可能会点击链接和在这里结束。
That question is closed, so I'll give the answer to it here: Yes, it is possible to use multiple arguments, as described in the documentation: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/property-let-statement
这个问题已经结束,所以我会在这里给出答案:是的,可以使用多个参数,如文档中所述:https: //docs.microsoft.com/en-us/office/vba/语言/参考/用户界面帮助/property-let-statement
You use a ParamArray declaration.
您使用 ParamArray 声明。