CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 1999
    Location
    Ohio, USA
    Posts
    163

    reusing error messages

    I have a program that has around 20 functions. For every function I have to copy and paste the same error messages. Is there a way I can reuse the error messages without pasting them in every function over and over? I thought about classes but I don’t know how to do it. Any suggestion would be appreciated.


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: reusing error messages

    Classes are good, I've used classes for this purpose before. We just created a class, which had a sub called ShowMessage, which took an id, errornumber and description. In the sub, we just took the ID, checked to see if it was a known error, if so, we showed or message, if not, we showed the number and message passed to the sub. It looked something like this:

    ' class clsErrHandler
    public Sub ShowMessage(ID as long, optional ErrNo as Long=0, optional ErrDesc as string="")

    Select Case ID
    Case 1
    strMessage = "error 1 occured"
    Case ... '
    Case else
    strMessage = "Following unknown error occured:" & vbcrlf & ErrNo & vbcrlf & ErrDesc
    End Select

    Msgbox strMessage, vbCritical, "error"

    End Sub

    '
    '
    ' When the program started, we declared a public instance of our class
    set ErrHandler = new clsErrHandler
    '
    '
    ' When we needed it
    private Sub TestErrHandler
    on error Goto Err_Handler
    Dim A as Integer
    A = 1 / 0
    Exit Sub

    Err_Handler:

    Select Case Err.Number
    Case 1
    ErrHandler.ShowMessage 1
    Case else
    ErrHandler.ShowMessage 0, Err.Number, Err.Description
    End Select

    End Sub



    Ok, this also implies you having to put errorhandling in every sub, but you won't have to retype all the messages, and if you change a message, it is changed everywhere, which doesn't require you to replace it all over the project.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: reusing error messages

    Another good reason for doing something like what you all are talking about is in your ShowMessage sub you can also write the errors to a log file so when users "don't remember the exact error" or in cases where the users simply ignore the error, you'll have your own record of it.


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: reusing error messages


    'You may also call in your error hanlder routine a public function of a bas
    'module (this will not prevent you from writing at least 5 sentences in each
    'sub or function)
    ie:
    private sub mySub()
    on error goto errHandler
    ...
    ...
    exit_from_here:
    ...
    exit sub
    errHandler:
    msgbox myPublicFunctionShowMyMessage(err.number,err.description)
    resume next 'or resume exit_from_here or what you want,
    'but resume something!
    end sub




    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured