Let/Get Properties 中的可选参数如何工作?

发布于 2024-11-19 20:00:59 字数 1205 浏览 3 评论 0原文

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

1)下面的代码可能吗?...
本质上我有两个枚举,将它们称为eDATASETeDATATSUBSETeDATASET 中的特定值应触发 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)在调用对象时如何将可选参数传递给可写属性...
除了看似向后放置的可选参数(与函数和子函数中的可选参数相比)之外,我很难找到有关此功能的任何文档。 vba 帮助是这样说的:

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

以及以下内容来自 vbusers.com。两者都没有在使用方式上解释太多。那么,当从代码模块调用对象时,我如何传递可选参数... oObj.foo = ???

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

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

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) 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:

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.

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) 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖阳 2024-11-26 20:00:59

1)是的您的代码是可能的。

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

假设 myObject 是您的类的对象:

myObject.foo(lngSubSet) = lngSuperSet 

arglist 中参数的位置确实看起来不错很奇怪,但这就是适合您的 VBA。假设您有 4 个参数,其中两个是可选的,再加上您的右侧。您可以这样放置它们:

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

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

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

3) 有更好的方法吗? 总是有是,取决于你问的是谁。您可以将 lngSubSet 参数完全作为一个单独的属性。我就是这么做的。但就您而言,您的做事方式可能适合您。我不知道,这很大程度上是一个品味问题,取决于您的具体应用。

1) Yes your code is possible.

2) This is how you pass an argument:

Assuming myObject is an object of your class:

myObject.foo(lngSubSet) = lngSuperSet 

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)

and use them like this (assuming you're opting out of 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.

傲世九天 2024-11-26 20:00:59

这个问题被标记为一个完全不同的问题的答案,“Let property of VBA class module - is it possible to have multiple argument? [duplicate]”,因此想要该问题答案的人可能会点击链接并到这里结束。

这个问题已经结束,所以我将在这里给出答案:是的,可以使用多个参数,如文档中所述:https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/property-let-statement

您使用 ParamArray 声明。

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.

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://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/property-let-statement

You use a ParamArray declaration.

输什么也不输骨气 2024-11-26 20:00:59

我使用的是 Excel 2016。

在很多情况下,使用 ParamArray 可能会变得不必要的复杂...

您需要确保 Let & Get 声明具有相同数量的参数和参数名称。如果获取&让属性使用不同的参数,然后需要 ParamArray。

'********************************************
'********************************************
Public Property Let col(ByVal j As Integer, _
                        Optional normalize_IO_vector As Boolean = False, _
                        row_vals() As Double)

    '
    ' do stuff
    '
    
End Property
'---------------
Public Property Get col(ByVal j As Integer, _
                        Optional normalize_IO_vector As Boolean = False) As Double()

    '
    ' do stuff
    '
    
End Property
'********************************************
'********************************************

下面的属性声明会引发错误...

' !! this DOES NOT WORK...
'
' "normalize_input_vector" VS "normalize_output_vector" 
'
'********************************************
'********************************************
Public Property Let col(ByVal j As Integer, _
                        Optional normalize_input_vector As Boolean = False, _
                        row_vals() As Double)

    '
    ' do stuff
    '
    
End Property
'---------------
Public Property Get col(ByVal j As Integer, _
                        Optional normalize_output_vector As Boolean = False) As Double()

    '
    ' do stuff
    '
    
End Property
'********************************************
'********************************************

在此处输入图像描述

I am using Excel 2016.

Using a ParamArray can be unnecessarily complicated for a lot of cases...

You need to make sure the Let & Get declarations have the same number of arguments AND argument names. If Get & Let properties use different arguments then the ParamArray is required.

'********************************************
'********************************************
Public Property Let col(ByVal j As Integer, _
                        Optional normalize_IO_vector As Boolean = False, _
                        row_vals() As Double)

    '
    ' do stuff
    '
    
End Property
'---------------
Public Property Get col(ByVal j As Integer, _
                        Optional normalize_IO_vector As Boolean = False) As Double()

    '
    ' do stuff
    '
    
End Property
'********************************************
'********************************************

The property declarations below throws an error...

' !! this DOES NOT WORK...
'
' "normalize_input_vector" VS "normalize_output_vector" 
'
'********************************************
'********************************************
Public Property Let col(ByVal j As Integer, _
                        Optional normalize_input_vector As Boolean = False, _
                        row_vals() As Double)

    '
    ' do stuff
    '
    
End Property
'---------------
Public Property Get col(ByVal j As Integer, _
                        Optional normalize_output_vector As Boolean = False) As Double()

    '
    ' do stuff
    '
    
End Property
'********************************************
'********************************************

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文