Results 1 to 11 of 11
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    OPTIONAL with type declaration

    Word97SR2

    I couldn't find a caveat against this in the Help file, so wondered if anyone else has noticed mildly-aberrant behaviours with the OPTIONAL keyword in the argument list of a procedure definition.

    In both examples I was running the little test as shown immediately below; always calling with no argument, that is, with the OPTIONAL argument missing.


    <pre>MsgBox strGetDrives()
    </pre>



    In the first code snippet below, the conditional evaluates to TRUE, whereas in the second example it evaluates to FALSE. The difference is that the second example has (awkwardly if not incorrectly) specified the OPTIONAL parameter as of a type!


    <pre>Public Function strGetDrives(Optional strIN) As String
    If IsMissing(strIN) Then
    </pre>


    <pre>Public Function strGetDrives(Optional strIN As String) As String
    If IsMissing(strIN) Then
    </pre>


  2. #2
    Matt Kinsella
    Guest

    Re: OPTIONAL with type declaration

    Chris,

    I believe that the code is actually working correctly - however, you are encountering the problem as the "IsMissing" test only works for data of type Variant. As you did not specify a data type in your first example, the default data type of Variant is applied, and therefore the test works as expected. However, in the second example, the parameter is defined as type String, and therefore IsMissing does not test it as expected.

    I hope that that makes sense,

    Matt

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: OPTIONAL with type declaration

    > the default data type of Variant is applied, and therefore the test works as expected.

    Matt, thanks for this. Now that you mention it, I do recall reading this somewhere, but had forgotten it.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: OPTIONAL with type declaration

    Any argument declared as Optional with a specified datatype defaults to the default value for that datatype unless you specify a default. So an optional string defaults to an empty string "", an Optional boolean defaults to False, and optional numeric type defaults to 0. Only variants can be Null or missing.
    Charlotte

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: OPTIONAL with type declaration

    Thanks, Charlotte. For me a basic rule is going to be "Don't mix Optional with anything".

    Either that or else I have to dream up a different test for "missing" arguments.

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: OPTIONAL with type declaration

    I almost never use IsMissing because I usually use optional variants only when I want to allow Nulls. No, I take that back. I do use IsMissing on routines that can either retrieve a value or set it to the passed argument, but that's about the only time. When you declare optional arguments as particular types, you can always test for 0, false, or "", depending on the type--not too difficult, especially since it avoids the dreaded "Invalid Use of Null" message.[img]/w3timages/icons/grin.gif[/img]
    Charlotte

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: OPTIONAL with type declaration

    Hmm. (puts on thinking cap). I'll have to reconsider this.


    I liked the idea of Optionals for several of my string functions such as my upper-case function which would take an optional boolean flag to force a case-sensitive search. In those sorts of cases (boolean) there's not a lot of room to manoevure.

    With a Sting or Long variable, I may have better choises. For example "" means It's optional, whereas " " means its empty (as my routines might see a paramater).

    Maybe an optional boolean flag is not such a big deal anyway.

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: OPTIONAL with type declaration

    Chris,

    I would have thought that it would be OK to use a variant as an optional. You then have the chance to check whether the variable is a type which you would expect, as well as checking if it were entered.

    So, you could have something like:

    <pre>
    Sub Test()
    Call sub1
    Call sub1("A")
    Call sub1(1.1)
    Call sub1(1)

    End Sub

    Sub sub1(Optional varParam1 As Variant)
    If IsMissing(varParam1) Then
    MsgBox "none"
    ElseIf Not IsNumeric(varParam1) Then
    MsgBox "not numeric"
    ElseIf VarType(varParam1) <> vbInteger Then
    MsgBox "Not integer"
    End If
    End Sub
    </pre>


    You can do all you want with optional parms, and have extra checking in there as well.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: OPTIONAL with type declaration

    Thanks, Geoff. I'd not considered that.

    Most (all?) of my procedures are designed to be used by me, or a clone of me. I always (?) know and use the correct type, so it's difficult for me to justify checking as you've portrayed it. When I start typing a call to a procedure, VBE prompts me with the dummy argument names, and since I've sued correct prefixes for those, I'm guided.

    Calls from internal procedures would always have the correct type - that's the old argument about checking data only on entry to the system and not afterwards.

    Optional Arguments seem more and more to belong to that class(ouch!) of procedures which are written by one person for the use of another unknown person.

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: OPTIONAL with type declaration

    Chris,

    >Optional Arguments seem more and more to belong to that class(ouch!) of procedures which are written by one person for the use of another unknown person.

    perhaps- but optional arguments are used so many times by MS, that they can't be all bad.

    I've suggested type checking just to keep it tight- but that doesn't mean you would have to do all of that.

    Optionals can be quite useful in keeping your code tidy. Using variants can tighten up on their use. Don't ignore their use just because you don't like the type checking. That was just optional stuff.

    2c worth anyway.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #11
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: OPTIONAL with type declaration

    Actually, I think of optional arguments as the lazy programmer's life saving device. I hate to build two routines, one to do something and one to tell me what's been done. So I usually create a routine that will do either one, depending on whether I pass it an optional argument. Then too, you can set the default value of an optional argument so that the only time you have to actually pass that argument is when you need the routine to do something different. Saves mileage on older fingers.[img]/S/grin.gif[/img]
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •