Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do optional Parameters in Let/Get Properties work?

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?

like image 344
Hari Seldon Avatar asked Jul 10 '11 16:07

Hari Seldon


People also ask

What is the use of optional parameter?

A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.

How do you pass optional parameters to a method?

By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.

Why are optional parameters are added?

Optional Parameters are parameters that can be specified, but are not required. This allows for functions that are more customizable, without requiring parameters that many users will not need.

What are optional parameters are added?

As the name suggests optional parameters are not compulsory parameters, they are optional. It helps to exclude arguments for some parameters. Or we can say in optional parameters, it is not necessary to pass all the parameters in the method. This concept is introduced in C# 4.0.


1 Answers

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.

like image 59
Jean-François Corbett Avatar answered Sep 18 '22 14:09

Jean-François Corbett