Resolved How do I pass the hierarchical structure of the current sub to another sub?

CodyB

Active member
Joined
Jul 23, 2007
Messages
26
Programming Experience
3-5
I'm making a library DLL with a standard error handler for all my code. I want to pass the hierarchical structure of the calling sub, for my error handler to post it back to me as a heading. Then when I get a message from my error handler I know exactly where to look without deciphering the nested InnerExceptions and StackTraces. I can manually enter it everytime I make a try/catch statement, but I'd rather it be handled with code to prevent copy/paste omissions. Here is what I got so far.


    Private Sub SetSizes(ByVal Caller As System.String)
        Try

            'Code to do work
 
        Catch ex As Exception
            ErrorHandler.ErrorMsg(ex, "MyLibrary.Controls.ctrFieldControl.SetSizes", Caller, ErrorHandler.ErrorTitle.CustomControl)
        End Try
    End Sub


"MyLibrary.Controls.ctrFieldControl.SetSizes" is the hierarchical structure of the above sub. Caller is the hierarchical structure of the sub that called this one.

Public Class ErrorHandler
    Overloads Shared Sub ErrorMsg(ByVal ex As System.Exception, ByVal MethodLocation As System.String, ByVal MethodCaller As System.String, ByVal Title As ErrorTitle)
        Dim strTitle As System.String = "Application"
        Select Case Title
            Case ErrorTitle.Application
                strTitle = "Application Error"
            Case ErrorTitle.Library
                strTitle = "Library Error"
            Case ErrorTitle.CustomControl
                strTitle = "CustomControl Error"
        End Select
        ErrorMsg(ex, MethodLocation, MethodCaller, strTitle)
    End Sub

    Overloads Shared Sub ErrorMsg(ByVal ex As System.Exception, ByVal MethodLocation As System.String, ByVal MethodCaller As System.String, ByVal Title As System.String)
        Dim LF As System.String = Microsoft.VisualBasic.ChrW(10)
        Dim dLF As System.String = LF & LF
        Dim Paragraph As System.String = MethodLocation
        If Strings.Trim(MethodCaller) <> "" Then Paragraph = Paragraph & LF & MethodCaller
        If ex.InnerException Is Nothing Then
            Paragraph = (Paragraph & dLF & ex.Message.ToString)
        Else
            If ex.InnerException.InnerException Is Nothing Then
                Paragraph = (Paragraph & dLF & ex.Message.ToString & dLF & ex.InnerException.ToString)
            Else
                If ex.InnerException.InnerException.InnerException Is Nothing Then
                    Paragraph = (Paragraph & dLF & ex.Message.ToString & dLF & ex.InnerException.ToString & dLF & ex.InnerException.InnerException.ToString)
                Else
                    Paragraph = (Paragraph & dLF & ex.Message.ToString & dLF & ex.InnerException.ToString & dLF & ex.InnerException.InnerException.ToString & dLF & ex.InnerException.InnerException.ToString)
                End If
            End If
        End If
        If Strings.Left(Paragraph, 2) = dLF Then Paragraph = Strings.Right(Paragraph, (Strings.Len(Paragraph) - 2))
        MsgBox(Paragraph, MsgBoxStyle.OkOnly, Title)
    End Sub

    Public Enum ErrorTitle As Integer
        Application = 0
        Library = 1
        CustomControl = 2
    End Enum
End Class


Any ideas?

Thanx for your time. :)
 
Last edited:
What you're asking for is actually called the fully qualified name. I can't think of anything more specific than this:
Dim method = New StackTrace().GetFrame(0).GetMethod()

MessageBox.Show(method.DeclaringType().FullName & "." & method.Name)
EDIT: Actually, you can just call MethodBase.GetCurrentMethod to get the MethodBase object rather than going via a StachFrame. You could simply pass that MethodBase object to your ErrorHandler and let it construct the full name.
 
Here's the new version.

    Private Sub TheCallingSub()     
        Dim MyMethod = New StackTrace().GetFrame(0).GetMethod()
        Dim MyQualifiedName As String = MyMethod.DeclaringType().FullName & "." & MyMethod.Name
        Try

            'Code to do work

            SetSizes(MyQualifiedName)

            'Code to do work

        Catch ex As Exception
            ErrorHandler.ErrorMsg(ex, MyQualifiedName, "", CLB.ErrorHandler.ErrorTitle.Library)
        End Try
    End Sub


In the calling sub I define MyQualifiedName before the Try so that MyQualifiedName is available to pass to SetSizes().

    Private Sub SetSizes(ByVal Caller As System.String)     
        Try

            'Code to do work

        Catch ex As Exception
            Dim MyMethod = New StackTrace().GetFrame(0).GetMethod()
            Dim MyQualifiedName As String = MyMethod.DeclaringType().FullName & "." & MyMethod.Name
            ErrorHandler.ErrorMsg(ex, MyQualifiedName, Caller, CLB.ErrorHandler.ErrorTitle.Library)
        End Try
    End Sub


In the destination sub I put it in the Catch. It's all copy/paste now. :)
Thank You :)
 
Last edited:
As I said in my edit, I realised that this:
Dim method = New StackTrace().GetFrame(0).GetMethod()
would be better done like this:
Dim method = MethodBase.GetCurrentMethod()
 
Back
Top