[Gambas-user] "Groups" of optional arguments
BB
adamnt42 at gmail.com
Wed Nov 30 09:04:27 CET 2022
Maybe I'm overthinking this or maybe my brain is just not as sharp as it
used to be.
Short explanation: I think I need to group optional arguments something
along the lines of :
Sub XYZ ( arg1 As String, Optional ( arg2 As Boolean, arg3 As Integer =
0), Optional ( arg4 As Date = Null, arg5 as Integer = -Inf ) )
The illogical thinking is that calls to the method
* must have arg1 provided and
* either
o arg2 and possibly arg3
* or
o possibly arg4 and possibly arg5
The example is simpified, btw. 😁
Is there a way to do this?
Long explanation: We have a hierarchical set of classes, where for
example, Region contains a collection of Variety which contains a
collection of Vintage. Also we have a set of little projects that let us
edit the static data for each of these classes. I want to be able to
invoke the editors from within each. So if I am running EditRegion I can
invoke EditVariety via a button in the EditRegion form. Alternatively,
for example, if I am running EditVintage I want a button to invoke
EditVariety and therein is the situation where I (think that I) need
this grouped optional arguments thing.
EditVariety.Run() "needs" to take two different sets of arguments
depending on whether I am coming "down" from EditRegion or "up" from
EditVintage. On the "down" pathway it needs to show a list of the
varieties produced for that region i.e. in EditRegion I want to call it
with
Shell "editvariety " & subst("-r &1", thisregionid)
i.e. the command is "editvariety -r Langhorne"
but on the way "up" I want to call it with
Shell "editvariety " & subst("-r &1 -v &2 -y &2" , thisregionid, thisvarietyid, thisvarietyyear )
i.e, the command is "editvariety -r Langhorne -v Shiraz -y 2018"
All straightforward so far, but...
I also want, for example, on the way up or down to get all the varietals
for that region, so
Shell "editvariety " & subst ( "-r &1 -y &2", thisregionid, thisvarietyyear )
ie the command is "editvariety -r Langhorne -y 2018"
Believe it or not, that was simplified just to be able to explain it.
The true situation is horribly more complex involving a lot more
arguments and argument "sets". I am currently doing this with a complete
mash of nested and un-nested if statements in each of the "calling"
editors. Result, spaghetti code and maintenance hell. Probably bad
design but its what we've got.
I have tried changing the editvariety's Run() to a variadic (is that
the word?) method i.e. Run ( region as string, ... ) but that just
replicated all the spaghetti in the calling programs into the
editvariety program. Gaargh!
I have also tried using -Inf as the default values for some of the
optional arguments 😂😂😂😭 but they tend to slip through into "somewhat
complex" logical operations, some of which involve "arithmetic", and
*that* has been well and truly squashed by those with more intelligence
than I.
regards
bruce (currently located in the kingdom of Confusion in the Citadel of
Toxic Options)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20221130/e44b25b3/attachment-0001.htm>
More information about the User
mailing list