Easy MsgBox!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Easy MsgBox!

  1. #1
    Join Date
    Sep 2006
    Posts
    32

    Easy MsgBox!

    I use MsgBox a lot in development to make sure that variables have the proper values before proceeding further.

    I wrote the following Sub to display up to 4 variables, and take care of bothersome keystrokes and converions!

    Please review, use, and suggest some improvements!




    ' Easy MsgBox
    Sub mb(String1 As Variant, Optional String2 As Variant, Optional String3 As Variant, Optional String4 As Variant)
    Dim txt$
    ' String1
    If IsMissing(String1) Then String1 = ""
    If IsNumeric(String1) Then String1 = Str$(String1)
    ' String2
    If IsMissing(String2) Then String2 = ""
    If IsNumeric(String2) Then String2 = Str$(String2)
    If String2 <> "" Then String2 = " " + String2
    ' String3
    If IsMissing(String3) Then String3 = ""
    If IsNumeric(String3) Then String3 = Str$(String3)
    If String3 <> "" Then String3 = " " + String3
    ' String4
    If IsMissing(String4) Then String4 = ""
    If IsNumeric(String4) Then String4 = Str$(String4)
    If String4 <> "" Then String4 = " " + String4
    ' display message
    txt$ = Trim$(String1 & String2 & String3 & String4)
    MsgBox (txt$)
    End Sub

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Easy MsgBox!

    On first thought, despite all rumours, optional parameters can also be other types than variant.
    Then, instead of using an If IsMissing(Param), you might use the default value definition which is optional to the optional parameter. It goes like
    Code:
    Sub mb(String1 As Variant = "", Optional String2 As Variant = "" , Optional String3 As Variant = "" , Optional String4 As Variant[ B]= ""[/B])
    This makes sure, all missing parameters are defaulted to the "" (empty string)&#180;, without you having to do an If IsMissing() for every parameter.

  3. #3
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,874

    Re: Easy MsgBox!

    Not in VB6... Declare them and then assign them to nothing.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Easy MsgBox!

    What do you mean, "not in VB6", David?
    I'm sure I can have something like:
    Code:
      Sub MySub(Optional StrPar as String = "abc")
        'gives StrPar the default value of "abc"
        debug.print StrPar
      End Sub
    You don't need the variant here. Only an IsMissing(StrPar) would never be true, even if the parameter is really not given in a call.

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,874

    Re: Easy MsgBox!

    I meant assigning to a variable when you declare it:
    Code:
    Dim StrPar as String = "abc"
    is VB.Net.

    Code:
    Dim StrPar as String
    StrPar = "abc"
    Is VB6...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Easy MsgBox!

    Oh yes. That's right. But that's not what I had written.
    I did no Dim, but just declared a parameter to a sub:
    Sub MySub(Optional StrPar as String = "abc")
    which is perfect VB6.

  7. #7
    Join Date
    Sep 2006
    Posts
    32

    Re: Easy MsgBox!

    I found a simpler code.
    Let Errors do the job!



    ' Easy MsgBox (up to four items of any type)
    Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
    Const divider = " "
    Dim message As String, V As Variant
    On Error GoTo ErrorHandler

    V = A: message = Trim$(V) & divider
    V = B: message = Trim$(message) & divider & Trim$(V)
    V = C: message = Trim$(message) & divider & Trim$(V)
    V = D: message = Trim$(message) & divider & Trim$(V)
    MsgBox (message)

    Exit Sub
    ErrorHandler:
    message = Trim$(message) & divider & Trim$(Str$(V))
    Resume Next
    End Sub


    Pls understand that "rumours optional parameters" are exactly what I need! I need a message of UP TO FOUR ITEMS!

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Easy MsgBox!


    I'm quite confident, the above code would run as well without any error handler.
    Since you are using the default assignment as I suggested
    Optional B As Variant = ""
    no error whatsoever is produced within your code
    V = A: message = Trim$(V) & divider
    V = B: message = Trim$(message) & divider & Trim$(V)
    V = C: message = Trim$(message) & divider & Trim$(V)
    V = D: message = Trim$(message) & divider & Trim$(V)
    MsgBox (message)


    So the error handler business is obsolete, I'd say.

    Also it seems somewhat strange:
    In the error handler you repeat exactly the same statement of which you obviously expected to produce an error. So you are producing an error again within the error handler?

  9. #9
    Join Date
    Sep 2006
    Posts
    32

    Re: Easy MsgBox!

    Thanks for the simplification!

    On second thought, I think dividers should stay, and changed to something like " | ", to show which parameters are missing.

    So, here is the latest V:


    ' Easy MsgBox (up to four items of any type)
    Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
    Const divider = " | "
    Dim message As String, V As Variant
    message = ""
    V = A: message = V
    V = B: message = message & divider & V
    V = C: message = message & divider & V
    V = D: message = message & divider & V
    MsgBox (Trim$(message))
    End Sub

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Easy MsgBox!

    Looks good.
    Good enough to finally start using code tags, anyway.

    What about forgetting V:
    Code:
    Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
    Const divider = " | "
    Dim message As String
    
    message = Trim$(A & divider & B & divider & C & divider & D)
    MsgBox message

  11. #11
    Join Date
    Sep 2006
    Posts
    32

    Re: Easy MsgBox!

    Even better!
    Thanks WoF!

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Easy MsgBox!

    Still want it shorter?
    Code:
    Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
      Const divider = " | "
      MsgBox Trim$(A & divider & B & divider & C & divider & D)
    End Sub

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center