optional argument - basic.visual

This is a discussion on optional argument - basic.visual ; Hi, I have a function with an optional argument (formobject type). When I call the function without the optional arg, but DO FILL IN the optional argument in the function. I do not get an error! Is that coincidance? The ...

+ Reply to Thread
Results 1 to 6 of 6

optional argument

  1. Default optional argument

    Hi,

    I have a function with an optional argument (formobject type). When I call
    the function without the optional arg, but DO FILL IN the optional argument
    in the function. I do not get an error! Is that coincidance? The returned
    argument can be used without problem.

    Public Function isFormActive(psFormname As String, Optional pFrm As Form) As
    Boolean
    set pFrm=someform
    ......
    isFormActive=true
    end function

    if isFormActive("formname") then
    ....

    Regards

    Frank



  2. Default Re: optional argument

    On Mon, 1 Dec 2003 11:15:53 +0100, Frank wrote:

    >I have a function with an optional argument (formobject type). When I call
    >the function without the optional arg, but DO FILL IN the optional argument
    >in the function. I do not get an error! Is that coincidance? The returned
    >argument can be used without problem.
    >
    >Public Function isFormActive(psFormname As String, Optional pFrm As Form) As
    >Boolean
    >set pFrm=someform
    >.....
    >isFormActive=true
    >end function
    >
    >if isFormActive("formname") then
    >...


    This is normal, and has nothing to do with it being optional. Arguments
    are normally passed by giving the called sub or function the memory
    address of the arg (aka by reference, which can be done explicitly (and
    unnecessarily) with ByRef - ByRef x As String, for example). Therefore,
    any changes made to those arguments are also made to any variables that
    were passed.

    If you don't want this to happen, then pass the args by value using
    ByVal, like this:
    Sub foo (ByVal x As String, ByVal y as Byte)

    This forces VB to make a copy of those args marked as ByVal, and then
    any changes happen to the copies, which are discarded when the sub or
    function terminates.
    --
    auric "underscore" "underscore" "at" hotmail "dot" com
    *****
    People who live in glass houses shouldn't.

  3. Default Re: optional argument


    "Auric__" <not.my.real@email.address> wrote in message
    news:jp5msvght679n6edqdd6gr691utnuc3bji@4ax.com...
    > On Mon, 1 Dec 2003 11:15:53 +0100, Frank wrote:
    >
    > >I have a function with an optional argument (formobject type). When I

    call
    > >the function without the optional arg, but DO FILL IN the optional

    argument
    > >in the function. I do not get an error! Is that coincidance? The returned
    > >argument can be used without problem.
    > >
    > >Public Function isFormActive(psFormname As String, Optional pFrm As Form)

    As
    > >Boolean
    > >set pFrm=someform
    > >.....
    > >isFormActive=true
    > >end function
    > >
    > >if isFormActive("formname") then
    > >...

    >
    > This is normal, and has nothing to do with it being optional. Arguments
    > are normally passed by giving the called sub or function the memory
    > address of the arg (aka by reference, which can be done explicitly (and
    > unnecessarily) with ByRef - ByRef x As String, for example). Therefore,
    > any changes made to those arguments are also made to any variables that
    > were passed.
    >
    > If you don't want this to happen, then pass the args by value using
    > ByVal, like this:
    > Sub foo (ByVal x As String, ByVal y as Byte)
    >
    > This forces VB to make a copy of those args marked as ByVal, and then
    > any changes happen to the copies, which are discarded when the sub or
    > function terminates.
    > --
    > auric "underscore" "underscore" "at" hotmail "dot" com
    > *****
    > People who live in glass houses shouldn't.


    Thats all very nice Auric, but I DON'T pass an argument and can still use
    the parameter in the function. And that is a bit strange, isn't it?
    Frank



  4. Default Re: optional argument

    > > >I have a function with an optional argument (formobject type). When I
    > call
    > > >the function without the optional arg, but DO FILL IN the optional

    > argument
    > > >in the function. I do not get an error! Is that coincidance? The

    returned
    > > >argument can be used without problem.
    > > >
    > > >Public Function isFormActive(psFormname As String, Optional pFrm As

    Form)
    > As
    > > >Boolean
    > > >set pFrm=someform
    > > >.....
    > > >isFormActive=true
    > > >end function
    > > >
    > > >if isFormActive("formname") then
    > > >...

    > >
    > > This is normal, and has nothing to do with it being optional. Arguments
    > > are normally passed by giving the called sub or function the memory
    > > address of the arg (aka by reference, which can be done explicitly (and
    > > unnecessarily) with ByRef - ByRef x As String, for example). Therefore,
    > > any changes made to those arguments are also made to any variables that
    > > were passed.
    > >
    > > If you don't want this to happen, then pass the args by value using
    > > ByVal, like this:
    > > Sub foo (ByVal x As String, ByVal y as Byte)
    > >
    > > This forces VB to make a copy of those args marked as ByVal, and then
    > > any changes happen to the copies, which are discarded when the sub or
    > > function terminates.
    > > --
    > > auric "underscore" "underscore" "at" hotmail "dot" com
    > > *****
    > > People who live in glass houses shouldn't.

    >
    > Thats all very nice Auric, but I DON'T pass an argument and can still use
    > the parameter in the function. And that is a bit strange, isn't it?
    > Frank


    Optional does **not** mean "it does not **exist** if no value is assigned to
    it"; it simply means that you have the choice of giving a value to it when
    calling the function or not. Think of the argument list as *specialized* set
    of Dim statements for the function (notice the similarities between the two,
    especially when using the As <Type> qualifiers). The variables listed in the
    argument list are "declared into existence" as local variables and are
    available for use within the function.

    Actually, I'm having trouble envisioning how you could possibly segment your
    function code in a way that didn't rely on the Optional argument as
    "existing", even if it was as simple a case as

    Function SomeFunc(Optional Z As Variant) As <Whatever>
    If IsMissing(Z) Then
    Z= SomeInitializingValue
    End If

    for a Variant or, for VB6,

    Function SomeFunc(Optional Z As Long = -1) As <Whatever>
    If Z = -1 Then Z = SomeInitialValue
    .......

    You use the Optional variables in code all the time. The fact that it is an
    Object for your case does not change the concept any. Functionally (no pun
    intended), the code you posted, assuming no argument was passed in, is no
    different than the following...

    Public Function isFormActive(psFormname As String) As Boolean
    Dim pFrm As Form
    Set pFrm = someform
    .....
    isFormActive = True
    End Function

    if isFormActive("formname") then
    ....

    Rick - MVP



  5. Default Re: optional argument

    "Frank" <frank@frank.com> wrote in message news:<bqf4cs$b44$1@news2.tilbu1.nb.home.nl>...
    > Hi,
    >
    > I have a function with an optional argument (formobject type). When I call
    > the function without the optional arg, but DO FILL IN the optional argument
    > in the function. I do not get an error! Is that coincidance? The returned
    > argument can be used without problem.
    >
    > Public Function isFormActive(psFormname As String, Optional pFrm As Form) As
    > Boolean
    > set pFrm=someform
    > .....
    > isFormActive=true
    > end function
    >
    > if isFormActive("formname") then
    > ...


    Optional parameters exist inside the procedure whether the caller
    passed them or not. If they are omitted then VB creates temporary
    variables to use during the procedure so in this case 'pFrm' will have
    a value of Nothing (because it is an object reference) if it was not
    passed.

  6. Default Re: optional argument

    : Thats all very nice Auric, but I DON'T pass an argument and can still use
    : the parameter in the function. And that is a bit strange, isn't it?

    No ... it's just you didn't pass a value a declared variable (what the
    optional part means), not that the existence of the variable was optional
    (ie if you don't use it, it does not exist). Thus you can reference or use
    the variable however you want in the routine -- it's just that the variable
    may or may not have initially contained data passed to the routine.

    --

    Randy Birch
    MVP Visual Basic
    http://www.mvps.org/vbnet/
    Please respond only to the newsgroups so all can benefit.




+ Reply to Thread

Similar Threads

  1. Optional Parameter
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 10-29-2007, 05:07 PM
  2. Replies: 9
    Last Post: 10-29-2007, 01:39 PM
  3. Replies: 1
    Last Post: 09-15-2006, 01:42 PM
  4. Optional argument in ::method and ::routine
    By Application Development in forum REXX
    Replies: 1
    Last Post: 08-12-2006, 07:30 AM
  5. Optional TLS
    By Application Development in forum Microsoft Exchange
    Replies: 0
    Last Post: 12-26-2005, 11:16 AM