?
Solved

pass optional error object

Posted on 2005-03-17
4
Medium Priority
?
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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