Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

pass optional error object

Posted on 2005-03-17
4
Medium Priority
?
166 Views
Last Modified: 2010-04-23
I rather new to VB.Net and am trying to make an  "Error Logging" module that I can use in many different programs.

The Parms for the main sub are as follows..
Public Sub Logger(ByVal inFunction As String, Optional ByVal inLogText As String = "", Optional ByVal inErr As ErrObject = ?????)
'   @parm inFunction String description of the function that is calling this.
'   @parm inLogText Optional String description to be added to the logged output
'   @parm inErr Optional Error Number

As you can see I cant figure out what to put in place of the question marks.
Everything I have tried has failed.

Is there some "Dummy" instance of an ErrObject that I can use for its "Default" value?
0
Comment
Question by:ScottParker
4 Comments
 
LVL 7

Accepted Solution

by:
Joe_Griffith earned 600 total points
ID: 13569511
When you use the optional qualifier and the parameter is an object then the default value can only be "Nothing":

Public Sub Logger(ByVal inFunction As String, Optional ByVal inLogText As String = "", Optional ByVal inErr As ErrObject = Nothing)

Using Err is not a good idea, however.  You need to rethink this whole thing.  While Err is supported for backward compatibility you need to look into structured exception handling and the exception class.
0
 
LVL 6

Assisted Solution

by:ctm5
ctm5 earned 400 total points
ID: 13571048
VB.NET lets you "catch" exceptions, and you are probably better off passing the exception to the Logger sub. An exception object gives you lots of information. You only get to the Catch block if an exception (i.e., error) happens.

So in a program function or sub, you would include a Try ... Catch block:

Private Sub MySub()
Try
....subroutine material goes here

Catch ex as Exception
    Logger("MySub","Ooops!",ex)
End Try

End Sub

And Logger's third parameter would be an Exception (default value = Nothing) instead of an ErrObject. One nice thing about the Exception object is that it has a StackTrace property, so you can tell what subroutine or function called the Logger function easily. Play around with the Exception object a bit and you'll see what it has to offer.

ctm5
0
 
LVL 10

Expert Comment

by:123654789987
ID: 13572849
U can set the ErrObject to Nothing..

But I think it is more appropriate to create it as Err object..

Public Sub Logger(ByVal inFunction As String, Optional ByVal inLogText As String = "", Optional ByVal inErr As Err = Nothing)

U can use the Err.GetException method to get the exception.
0
 
LVL 3

Author Comment

by:ScottParker
ID: 13573963
Thanks.. I changed it to...

 Public Sub Logerror(ByVal inFunction As String, Optional ByVal inLogText As String = "", Optional ByVal inErr As Exception = Nothing)
        ' This is the main function called from other programs.
        '   @parm inFunction String description of the function that is calling this.
        '   @parm inLogText Optional String description to be added to the logged output
        '   @parm inErr Optional Exception
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question