dbTalk Databases Forums  

Use of "Optional" parameters in a Sub/Function?

comp.databases.ms-access comp.databases.ms-access


Discuss Use of "Optional" parameters in a Sub/Function? in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
(Pete Cresswell)
 
Posts: n/a

Default Use of "Optional" parameters in a Sub/Function? - 01-03-2005 , 03:32 PM






I've dabbled in "Optional" over the last few days and think I'm coming down
against using it.

Seems to me like it makes the code harder to read and more complicated.

Instead of using Optional, I'm making the parm a Variant and passing Null when
it's not used.... then checking for "If Len(theParmValue & "") > 0... to see if
it is present.

That way I can eliminate checks for IsMissing(theParmValue)...


Anybody else have thoughts on this?
--
PeteCresswell

Reply With Quote
  #2  
Old   
Douglas J. Steele
 
Posts: n/a

Default Re: Use of "Optional" parameters in a Sub/Function? - 01-03-2005 , 04:13 PM






Sorry, but I fail to see how If Len(theParmValue & "") > 0 is any easier to
read than If IsMissing(theParmValue).

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)



"(Pete Cresswell)" <x@y.z> wrote

Quote:
I've dabbled in "Optional" over the last few days and think I'm coming
down
against using it.

Seems to me like it makes the code harder to read and more complicated.

Instead of using Optional, I'm making the parm a Variant and passing Null
when
it's not used.... then checking for "If Len(theParmValue & "") > 0... to
see if
it is present.

That way I can eliminate checks for IsMissing(theParmValue)...


Anybody else have thoughts on this?
--
PeteCresswell



Reply With Quote
  #3  
Old   
David W. Fenton
 
Posts: n/a

Default Re: Use of "Optional" parameters in a Sub/Function? - 01-03-2005 , 06:02 PM



Chuck Grimsby <c.grimsby (AT) worldnet (DOT) att.net.invalid> wrote in
news:t7kjt0thvrjvjvhbn7g7m462tp7lfq9uqt (AT) 4ax (DOT) com:

Quote:
Use the Optional keyword with a = "setting" if there isn't a
input. For example:

Public Function GetASetting( _
SettingToGet As String, _
Optional TableName As String = "USYS_tblSettings", _
Optional whatDB As DAO.Database = Nothing) _
As Variant

If whatever calls this function doesn't supply a TableName,
"USYS_tblSettings" is used by default.

Saves all that nasty checking for IsMissing... You do still have
to check for invalid stuff passed to your function or sub however!

I've often added optional arguments to version 2 of a commonly-used
function/sub. In that case, it's pretty important to use it, as
otherwise, existing code will break.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc


Reply With Quote
  #4  
Old   
Eric Schittlipz
 
Posts: n/a

Default Re: Use of "Optional" parameters in a Sub/Function? - 01-03-2005 , 06:55 PM




"(Pete Cresswell)" <x@y.z> wrote

Quote:
I've dabbled in "Optional" over the last few days and think I'm coming
down
against using it.

Seems to me like it makes the code harder to read and more complicated.

Instead of using Optional, I'm making the parm a Variant and passing Null
when
it's not used.... then checking for "If Len(theParmValue & "") > 0... to
see if
it is present.

That way I can eliminate checks for IsMissing(theParmValue)...


Anybody else have thoughts on this?
--
PeteCresswell

If there were no such thing as optional parameters, think how different your
coding would look. Some of the things you could no longer write would
include:
DoCmd.Close (You now write DoCmd.Close Null, Null, Null)
DoCmd.OpenForm "frmWhatever" (You now write DoCmd.OpenForm "frmWhatever",
Null, Null, Null, Null, Null, Null)
OK, these are methods of built-in objects, not your custom functions, but
doesn't the same principal apply? I don't see how forcing users of your
functions (yourself?) to always provide every parameter would increase
readability or fail to irritate. Perhaps, though, you are thinking of a
specific function and whether parameters should be optional in that
particular case.




Reply With Quote
  #5  
Old   
david epsom dot com dot au
 
Posts: n/a

Default Re: Use of "Optional" parameters in a Sub/Function? - 01-04-2005 , 01:39 AM



The "Optional Parameter" is a hang-over from the way a couple
of dudes implemented their macro-assembler on a PDP mini-computer
decades ago. It is indeed a nifty feature, even if it did
fall out of a historical accident, but it is incompatible with
most current ideas about computer language design (Pearl excepted:
the designer of Perl is strongly supportive of irregular forms
for common verbs like DoCmd.OpenForm).

Ironic isn't it, that VB got optional parameters by aping C,
but C++ and OLE don't really have optional parameters any more?

In Access, optional parameters should default to Null, not
Missing (think about it!), but the fact is that Null is not
and cannot be a defined concept: we often wish Null to map
to 0 and "" and Empty and Error and Missing, and we often
wish Null to /NOT/ map to 0 or "" or Empty or Error or Missing:

there is no universal answer to some questions.

(david)


"(Pete Cresswell)" <x@y.z> wrote

Quote:
I've dabbled in "Optional" over the last few days and think I'm coming
down
against using it.

Seems to me like it makes the code harder to read and more complicated.

Instead of using Optional, I'm making the parm a Variant and passing Null
when
it's not used.... then checking for "If Len(theParmValue & "") > 0... to
see if
it is present.

That way I can eliminate checks for IsMissing(theParmValue)...


Anybody else have thoughts on this?
--
PeteCresswell



Reply With Quote
  #6  
Old   
Trevor Best
 
Posts: n/a

Default Re: Use of "Optional" parameters in a Sub/Function? - 01-05-2005 , 07:00 AM



(Pete Cresswell) wrote:
Quote:
I've dabbled in "Optional" over the last few days and think I'm coming down
against using it.

Seems to me like it makes the code harder to read and more complicated.

Instead of using Optional, I'm making the parm a Variant and passing Null when
it's not used.... then checking for "If Len(theParmValue & "") > 0... to see if
it is present.

That way I can eliminate checks for IsMissing(theParmValue)...


Anybody else have thoughts on this?
I think it's (optional) useful and allows a default value to be passed
in rather than the usual nulls, zeros, zero length strings, etc.

--
This sig left intentionally blank


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.