Results 1 to 11 of 11
  1. #1
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function lngIndexToListBox(lb As ListBox, st

    Chris,

    for readability/maintainability:

    .boolCase has not been defined, so therefore is Variant (I assume so that the "IsMissing" works OK). It might be clearer to define it explicitly as such- and even to name it "varUpperCase". I'd put a default in as well, to cut out the "IsMissing" test:
    optional varUpperCase = "N"

    I'd suggest that a varaibale called "bool" might be better named "boolUpperCase" (or even blnUpperCase if we followed the 3-character prefix)
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

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

    Code: Function lngIndexToListBox(lb As ListBox, st

    <pre>Public Function lngIndexToListBox(lb As ListBox, strText As String,_
    Optional boolCase) As Long
    ' Procedure : lngIndexToListBox
    ' Description: Return the ListIndex of a string in a Listbox.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: None
    ' Returns: LONG -1 if not found
    ' Assumes: Nothing
    ' Side Effects:
    ' Tested: By a call from the user.

    Dim bool As Boolean
    If IsMissing(boolCase) Then
    bool = False
    Else
    If UCase(boolCase) = "Y" Then
    bool = True
    Else
    bool = False
    End If
    End If
    lngIndexToListBox = -1 ' default result is failure
    Dim lngI As Long ' loop counter
    For lngI = 0 To lb.ListCount - 1
    If bool Then
    If lb.List(lngI) = strText Then
    lngIndexToListBox = lngI
    Exit For
    Else
    End If
    Else
    If UCase(lb.List(lngI)) = UCase(strText) Then
    lngIndexToListBox = lngI
    Exit For
    Else
    End If
    End If
    Next lngI

    End Function
    </pre>


  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: Code: Function lngIndexToListBox(lb As ListBox, st

    Geoff, thanks for the feedback which has me confused again ....

    > .boolCase has not been defined,

    eh? I thought that declaring it Optional was a declaration? I could undersnad naming it as a var; I'd thought that a bool prefix sort-of showed that I was *hoping* for a boolean as a parameter.

    > optional varUpperCase = "N"

    This goes in the function header? I didn't know I could do that.


    >blnUpperCase if we followed the 3-character prefix)

    Here's something else. I thought bool was common. I can switch to bln if that's generall more standard.

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

    Re: Code: Function lngIndexToListBox(lb As ListBox, st

    I'm going to have to ask the moderator to do something about these unwrapped lines (vbgrin!)


    >Optional blnCase As boolean = false)


    Now THIS is what I want! I rarely have a truly don't-know-what-it-is argument. My optionals are optional for the code-writer - me - so it's rarely that I don't know what an argument will be if I use it. I like the idea of being able to declare it as boolean. The default value is a bonus.


    Now the main body of the code can do its job without petty house-keeping up front. That makes the code more readable.

    Thanks. (wanders off to do a global search for "optional" in a gazillion modules spread across 40G of hard drive space ....)

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

    Re: Code: Function lngIndexToListBox(lb As ListBox, st

    Chris,

    actually, the function declaration could be more like:

    Public Function lngIndexToListBox(lb As ListBox, strText As String, _
    Optional blnCase As boolean = false) As Long

    Then you wouldn't neeed to test for "IsMissing"- indeed, you couldn't- because IsMissing works only for variants. If it's not defined, then it defaults to what you've set it to.

    So the code might now look like:
    <pre>Public Function lngIndexToListBox(lb As ListBox, strText As String, _
    Optional blnCaseSensitive As Boolean = False) As Long
    ' Procedure : lngIndexToListBox
    ' Description: Return the ListIndex of a string in a Listbox.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: None
    ' Returns: LONG -1 if not found
    ' Assumes: Nothing
    ' Side Effects:
    ' Tested: By a call from the user.

    lngIndexToListBox = -1 ' default result is failure
    Dim lngI As Long ' loop counter
    For lngI = 0 To lb.ListCount - 1
    If blnCaseSensitive Then
    If lb.List(lngI) = strText Then
    lngIndexToListBox = lngI
    Exit For
    Else
    End If
    Else
    If UCase(lb.List(lngI)) = UCase(strText) Then
    lngIndexToListBox = lngI
    Exit For
    Else
    End If
    End If
    Next lngI

    End Function
    </pre>


    boo, bol, bln might be vlaid- I just thought the standard was 3 characters not 4. As long as it's consistent.

    Declaring as optional is not a declaration. It can be Optional Integer, or any other type.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

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

    Re: Code: Function lngIndexToListBox(lb As ListBox, st

    <hr>I'm going to have to ask the moderator to do something about these unwrapped lines <hr>
    What do you maean? There's none of those long lines, causing problems whenever anybody views the thread.

    Not any more, anyway.

    Winnie, you have to lighten up. Use <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=scut&Number=26510&page= 0&view=expanded&sb=5&vc=1#Post26510>this post</A>, while it's still alive, to get a lighter sig
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  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: Code: Function lngIndexToListBox(lb As ListBox, st

    I have been upgrading my functions with OPTIONAL parameters, based on your feedback. each such function sees the removal of four or more lines of code (Good!) and a reduction in nesting of IF statements by one (Gooder!).



    Then I found a function where my IsMissing default value is not a constant, but is to be obtained from the INI files. By a functiuon call, of course.

    In the code snippet below, I had the header changed to

    <pre>Public Function strGetDrives(Optional strIN As String = strGp(strcApplication,_
    strcApplication, strcDriveLetters, strcLowerAlpha)) As String
    </pre>


    This generated a "recursion" compile error, which puzzled me only briefly. After all, maybe I *do* have a call to strGetDrives in my "read from INI" files. You'd half expect me to be checking what drives are available as part of an access to the file system. But on closer examination, no such reference.

    I experimented then with a cheap function:
    <pre>Public Function strDr() as String
    strDr="cde"
    End Function
    </pre>


    and hence
    <pre>Public Function strGetDrives(Optional strIN As String = strDr()) As String
    </pre>



    But this too generates the compile error message.

    I am currently of the opinion that
    (1) you can't have a value-returning function as a default value in a procedure declaration and
    (2) VBE gets a little confused from time to time. Or at least, there's an error-number poorly coded in the VBE.

    <pre>Public Function strGetDrives(Optional strIN As String = "c") As String
    ' Procedure : strGetDrives
    ' Description: Return a valid set of drive letters.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: Optional string of letters.
    ' Returns: String of valid drive letters.
    ' Assumes: None.
    ' Side Effects: None
    ' Tested: By the calls shown below.
    ' If the optional parameter is absent, we obtain a default from the INI file.
    ' We store the result in the INI file, regardless of the optional parameter.
    Dim strDr As String
    strDr = strIN
    ' If IsMissing(strIN) Then
    ' str = strGp(strcApplication, strcApplication,_
    strcDriveLetters, strcLowerAlpha)
    ' Else
    ' str = strIN
    ' End If
    (snip)
    </pre>


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

    Re: Code: Function lngIndexToListBox(lb As ListBox, st

    Here's another maybe-interesting facet of OPTIONAL.

    In the function below I use the ABSCENCE of the parameter as useful information. If the parameter is missing, I don't invent a default value in the code, I use the absence to control the logic.

    If I made the default an empty string ("") the very presence of that value tells the logic to DELETE the existing text, rather than append to the end.


    <pre>Public Function strGetHeaderFooterText(lngHeaderType As Long, _
    Optional strNewText) As String
    ' Procedure : strGetHeaderFooterText
    ' Description: Return the text from the header/footer, optionally replace the text.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: Header/Footer type,
    ' [optional new text]
    ' Returns: STRING of original text
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    Call boolOpenHeaderFooter(lngHeaderType)
    Selection.EndKey Unit:=wdStory, Extend:=wdExtend ' select the pane
    strGetHeaderFooterText = Selection.Text ' preserve the text
    If IsMissing(strNewText) Then ' Nothing else to do
    Else
    If Len(strNewText) = 0 Then
    Selection.Delete
    Else
    Selection.TypeText (strNewText)
    End If
    End If
    Call boolCloseHeaderFooter
    'Sub TESTstrGetHeaderFooterText()
    ' MsgBox strGetHeaderFooterText(wdSeekCurrentPageHeader, "new text")
    ' MsgBox strGetHeaderFooterText(wdSeekCurrentPageHeader, "")
    ' MsgBox strGetHeaderFooterText(wdSeekCurrentPageHeader)
    'End Sub
    End Function
    </pre>


  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: Code: Function lngIndexToListBox(lb As ListBox, st

    Here's another thing I can't do with default values. Darn! I had always throught of "PathSeparator" as a constant. It's not. It's a Property.


    <pre>Public Function strTemplatePath(Optional strDelim = _
    Application.PathSeparator) As String
    </pre>


  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: Code: Function lngIndexToListBox(lb As ListBox, st

    Chris,

    I had not even realised that the default value could be a variable. Perhaps it can't.

    It may be that a guideline to use would be to put a default value in only when it's constant. It may be safer.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

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

    Re: Code: Function lngIndexToListBox(lb As ListBox, st

    Pity, eh?

    This "late binding" seems to me what used to be called run-time, and if so, it ought to be possible to collect a default value at run-time (in this case from a property) at the usual cost in time-of-execution.

Posting Permissions

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