-
Wrapper functions for functions with optional parameters.
Hi -
I am wondering how people write wrapper functions for functions which have
optional parameters, preserving the "optionality" of each parameter. The
only way I can see this being done is by checking which parameters are "missing",
and providing a whole load of possible function calls based on this. The
problem is that the potential number of function calls will be n! - a nightmare
if you have more than three parameters.
--
Mark Alexander Bertenshaw
Programmer/Analyst
Chordiant Software, Inc.
Brentford
UK
-
Re: Wrapper functions for functions with optional parameters.
Mark,
we always use initialized optional parameters, like
function xyz (optional abc as string = "")
For use it removes the need for the IsMissing function and it also solves
the problem of wrapping .bas methods in GMU classes.
(BTW, for ParamArrays we make two functions : one which explicitly takes
an array and one which takes a paramarry (much like printf and vprintf in
C))
(BTW2 : never use optional xyz as variant = null to initialize a an optional
variant parameter. It gives something like "Method uses an automation type
not supported in VB" - only while compiling, not in the IDE)
Hope this helps,
Van den Driessche Willy.
-
Re: Wrapper functions for functions with optional parameters.
"Willy Van den Driessche" <Willy.van.den.driessche@skynet.be> wrote:
>
>Mark,
Thanks for replying, Willy.
>
>we always use initialized optional parameters, like
>function xyz (optional abc as string = "")
>
>For use it removes the need for the IsMissing function and it also solves
>the problem of wrapping .bas methods in GMU classes.
Yes - I normally do exactly this. However, in this case, a string "" or
a value 0 would actually do _bad_things_ if I sent it to the real function.
Incidentally, the actual situation I am in is wrapping up the Add methods
of the Nodes collection of a TreeView control.
i.e.
Public Function Add( _
Optional ByVal Relative As Variant, _
Optional ByVal Relationship As Variant, _
Optional ByVal Key As Variant, _
Optional ByVal Text As Variant, _
Optional ByVal Image As Variant, _
Optional ByVal SelectedImage As Variant _
) As CNode
Dim ccnodeAdded As Node
If IsMissing(Image) And IsMissing(SelectedImage) Then
Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text)
ElseIf IsMissing(Image) Then
Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text,
, SelectedImage)
ElseIf IsMissing(SelectedImage) Then
Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text,
Image)
Else
Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text,
Image, SelectedImage)
End If
Set Add = New CNode
Add.Initialise Me, ccnodeAdded
End Function
Funnily enough, it seems perfectly OK to pass the first two Variant parameters,
if they are missing. But the last two really do have to be set to something
meaningful, or not at all.
>(BTW, for ParamArrays we make two functions : one which explicitly takes
>an array and one which takes a paramarry (much like printf and vprintf in
>C))
Yeah! That's what I have been doing recently as well.
>(BTW2 : never use optional xyz as variant = null to initialize a an optional
>variant parameter. It gives something like "Method uses an automation type
>not supported in VB" - only while compiling, not in the IDE)
Thanks for that one - I'll keep it in mind.
>Hope this helps,
>Van den Driessche Willy.
--
Mark Alexander Bertenshaw
Programmer/Analyst
Chordiant Software, Inc.
Brentford
UK
-
Re: Wrapper functions for functions with optional parameters.
"Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote in message <news:3bbd8073$1@news.devx.com>...
> Hi -
>
> I am wondering how people write wrapper functions for functions which have
> optional parameters, preserving the "optionality" of each parameter. The
> only way I can see this being done is by checking which parameters are "missing",
> and providing a whole load of possible function calls based on this. The
> problem is that the potential number of function calls will be n! - a nightmare
> if you have more than three parameters.
Doesn't IntelliSense display the default values for optional typed
parameters? For optional variants, the value for which IsMissing
returns True can be passed around just like anything else.
--
Joe Foster <mailto:jlfoster%40znet.com> Sacrament R2-45 <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!
-
Re: Wrapper functions for functions with optional parameters.
"Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote:
>
>Hi -
>
>I am wondering how people write wrapper functions for functions which have
>optional parameters, preserving the "optionality" of each parameter. The
>only way I can see this being done is by checking which parameters are "missing",
>and providing a whole load of possible function calls based on this. The
>problem is that the potential number of function calls will be n! - a nightmare
>if you have more than three parameters.
>
>--
>Mark Alexander Bertenshaw
>Programmer/Analyst
>Chordiant Software, Inc.
>Brentford
>UK
Not quite sure what you are trying to do. (Of course that doesn't stop me
from having an opinion. <g>)
Usually providing a Function with Optional args, is the 'wrapper' not the
other way around. I can see where you might want to do something like wrap
the MsgBox with "MyErrorMsg( strError, strType, strLine )" and the subsequent
processing assembles an error string and error title for the message box.
This is called specification, or subclassing, and in general creates an 'narrower'
view.
Optional Parameters work best when they are only 'attributes' or qualities
of a 'has a' relationships and simple 'defaults' are available. ie., MsgBox
- no title? that is Ok I will use the default, ADO.Open - no cursor, Ok I
will use clientside, etc.
If however you attempt to use Optional Parameters as "Parameter Polymorphism",
that is, to dictate separate lines of processing (an 'is a' relationship)
then you are better off creating a single interface and separate classes.
This will usually show up if you are tempted to place a 'case' statement
in your code based on a particular parameter.
-
Re: Wrapper functions for functions with optional parameters.
"Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote:
>
>"Willy Van den Driessche" <Willy.van.den.driessche@skynet.be> wrote:
>>
>>Mark,
>
>Thanks for replying, Willy.
>
>>
>>we always use initialized optional parameters, like
>>function xyz (optional abc as string = "")
>>
>>For use it removes the need for the IsMissing function and it also solves
>>the problem of wrapping .bas methods in GMU classes.
>
>
>Yes - I normally do exactly this. However, in this case, a string "" or
>a value 0 would actually do _bad_things_ if I sent it to the real function.
> Incidentally, the actual situation I am in is wrapping up the Add methods
>of the Nodes collection of a TreeView control.
>
>i.e.
>
>Public Function Add( _
> Optional ByVal Relative As Variant, _
> Optional ByVal Relationship As Variant, _
> Optional ByVal Key As Variant, _
> Optional ByVal Text As Variant, _
> Optional ByVal Image As Variant, _
> Optional ByVal SelectedImage As Variant _
> ) As CNode
>Dim ccnodeAdded As Node
>
> If IsMissing(Image) And IsMissing(SelectedImage) Then
> Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text)
> ElseIf IsMissing(Image) Then
> Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text,
>, SelectedImage)
> ElseIf IsMissing(SelectedImage) Then
> Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text,
>Image)
> Else
> Set ccnodeAdded = m_oCCNodes.Add(Relative, Relationship, Key, Text,
>Image, SelectedImage)
> End If
>
> Set Add = New CNode
> Add.Initialise Me, ccnodeAdded
>
>End Function
>
>Funnily enough, it seems perfectly OK to pass the first two Variant parameters,
>if they are missing. But the last two really do have to be set to something
>meaningful, or not at all.
>
>>(BTW, for ParamArrays we make two functions : one which explicitly takes
>>an array and one which takes a paramarry (much like printf and vprintf
in
>>C))
>
>Yeah! That's what I have been doing recently as well.
>
>>(BTW2 : never use optional xyz as variant = null to initialize a an optional
>>variant parameter. It gives something like "Method uses an automation
type
>>not supported in VB" - only while compiling, not in the IDE)
>
>Thanks for that one - I'll keep it in mind.
>
>>Hope this helps,
>>Van den Driessche Willy.
>
>--
>Mark Alexander Bertenshaw
>Programmer/Analyst
>Chordiant Software, Inc.
>Brentford
>UK
Awww! Now I see.
What you really have is SelImageNode.Add and ImageNode.Add, etc.
-
Re: Wrapper functions for functions with optional parameters.
If you're using typed optionals, then there really isn't a problem. If
you're using Variant optionals, you don't have to sweat it either. The
'IsMissing' value is stored in a Variant that can be passed like any other.
The Variant value is VT_ERROR/DISP_E_PARAMNOTFOUND. CVErr only lets you set
up to 65k, and DISP_E_PARAMNOTFOUND is above that range, so you can't create
one of these with CVErr, but you can do it with the following function:
Function MissingVariant(ByVal Optional DontPass As Variant) As Variant
MissingVariant = DontPass
End Function
IsMissing(MissingVariant) returns True.
"Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote in message
news:3bbd8073$1@news.devx.com...
>
> Hi -
>
> I am wondering how people write wrapper functions for functions which have
> optional parameters, preserving the "optionality" of each parameter. The
> only way I can see this being done is by checking which parameters are
"missing",
> and providing a whole load of possible function calls based on this. The
> problem is that the potential number of function calls will be n! - a
nightmare
> if you have more than three parameters.
>
> --
> Mark Alexander Bertenshaw
> Programmer/Analyst
> Chordiant Software, Inc.
> Brentford
> UK
-
Re: Wrapper functions for functions with optional parameters.
Unfortunately this doesn't appear to help in this case.
Looking at Mark's second post treeview's Nodes.Add method is unhappy with
even the existance of a parameter that contains the 'Missing' value.
It's IDL looks normal enough:
HRESULT Add(
[in, optional] VARIANT* Relative,
[in, optional] VARIANT* Relationship,
[in, optional] VARIANT* Key,
[in, optional] VARIANT* Text,
[in, optional] VARIANT* Image,
[in, optional] VARIANT* SelectedImage,
[out, retval] INode** ppNode);
It's odd.
--
Anthony Jones
Nuesoft Ltd
-
Re: Wrapper functions for functions with optional parameters.
"Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote in message <news:3bbd8073$1@news.devx.com>...
> I am wondering how people write wrapper functions for functions which have
> optional parameters, preserving the "optionality" of each parameter. The
> only way I can see this being done is by checking which parameters are "missing",
> and providing a whole load of possible function calls based on this. The
> problem is that the potential number of function calls will be n! - a nightmare
> if you have more than three parameters.
This is a very minor nit, but wouldn't it be more like 2^n?
sub wrapper(optional a, optional b, optional c, optional d)
if ismissing(a) then
if ismissing(b) then
if ismissing(c) then
if ismissing(d) then call wrapped _
else wrapped d:=d
else ' c is present
if ismissing(d) then wrapped c:=c _
else wrapped c:=c, d:=d
endif
else ' b is present
if ismissing(c) then
if ismissing(d) then wrapped b:=b _
else wrapped b:=b, d:=d
else ' c is present
if ismissing(d) then wrapped b:=b, c:=c _
else wrapped b:=b, c:=c, d:=d
endif
endif
else ' a is present
if ismissing(b) then
if ismissing(c) then
if ismissing(d) then wrapped a:=a _
else wrapped a:=a, d:=d
else ' c is present
if ismissing(d) then wrapped a:=a, c:=c _
else wrapped a:=a, c:=c, d:=d
endif
else ' b is present
if ismissing(c) then
if ismissing(d) then wrapped a:=a, b:=b _
else wrapped a:=a, b:=b, d:=d
else ' c is present
if ismissing(d) then wrapped a:=a, b:=b, c:=c _
else wrapped a:=a, b:=b, c:=c, d:=d
endif
endif
endif
end sub
--
Joe Foster <mailto:jlfoster%40znet.com> DC8s in Spaace: <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!
-
Re: Wrapper functions for functions with optional parameters.
"Joe "Nuke Me Xemu" Foster" <joe@bftsi0.UUCP> wrote in message
news:3bbf2659@news.devx.com...
> "Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote in message
<news:3bbd8073$1@news.devx.com>...
>
> > I am wondering how people write wrapper functions for functions which
have
> > optional parameters, preserving the "optionality" of each parameter.
The
> > only way I can see this being done is by checking which parameters are
"missing",
> > and providing a whole load of possible function calls based on this.
The
> > problem is that the potential number of function calls will be n! - a
nightmare
> > if you have more than three parameters.
>
> This is a very minor nit, but wouldn't it be more like 2^n?
Er, yeah! Good spot.
--
Mark Alexander Bertenshaw
Programmer/Analyst
Chordiant Software
Brentford
UK
-
Re: Wrapper functions for functions with optional parameters.
"Anthony Jones" <yadayadayada@msn.com> wrote in message
news:3bbeb9e4@news.devx.com...
> Unfortunately this doesn't appear to help in this case.
>
> Looking at Mark's second post treeview's Nodes.Add method is unhappy with
> even the existance of a parameter that contains the 'Missing' value.
>
> It's IDL looks normal enough:
>
> HRESULT Add(
> [in, optional] VARIANT* Relative,
> [in, optional] VARIANT* Relationship,
> [in, optional] VARIANT* Key,
> [in, optional] VARIANT* Text,
> [in, optional] VARIANT* Image,
> [in, optional] VARIANT* SelectedImage,
> [out, retval] INode** ppNode);
>
> It's odd.
>
> --
> Anthony Jones
> Nuesoft Ltd
Matt & Anthony -
Dash it! It looks like I was wrong in what I said re. the Add method. I
must have been doing something very, very wrong, or I've been eating the
wrong mushrooms ...
Sorry for wasting your time.
--
Mark Alexander Bertenshaw
Programmer/Analyst
Chordiant Software
Brentford
UK
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks