Solved

How to get Class and Procedure name?

Posted on 2004-10-28
170 Views
Last Modified: 2010-04-23
How can I get Class, procedure that is inside the class, function, property... signature (name, their parameters, return type)?

Public Class MyClass

    Dim ClassName As String = Here some CRL function to return string MyClass as it is my Class

    Public Sub MySub()
        Dim ProcName As String = Here some CRL function to return string MySub as it is my procedure
        ' Some processing
    End Sub

End Class

Same thing applies with Property, Function, or any other objects.

Thanks.
0
Question by:KarcOrigin
    8 Comments
     
    LVL 4

    Accepted Solution

    by:
    Hi,

    The thing you are talking about is called Reflection, let's have a look at the System.Reflection Namespace !

    For example for your Sub :

    Dim MethodName As String = System.Reflection.MethodInfo.GetCurrentMethod.Name

    Glom
    0
     
    LVL 4

    Expert Comment

    by:Glom
    And for your class, you cannot initialize classname in its declaration section.
    Use the constructor instead

    Public ClassName As String

    Public Sub New()
        ClassName = Me.GetType().ToString()
    End Sub
    0
     
    LVL 5

    Author Comment

    by:KarcOrigin
    It works thanks a lot but can something like below can be done?

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            MsgBox(GetCurrentMethodName()) <-- This should return "btnStart_Click"
            MySub()
        End Sub

        Private Sub MySub()
            MsgBox(GetCurrentMethodName()) <-- This should return "MySub"
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MsgBox(GetCurrentMethodName()) <-- This should return "Form1_Load"


        End Sub

        Private Function GetCurrentMethodName() As String
            ** Use something to return the name of the method or sub from where it is been called
        End Function

    End Class

    Thanks
    0
     
    LVL 4

    Expert Comment

    by:Glom
    Unfortunately, you cannot know which method called the GetCurrentMethodName method.
    I think in this case you must use each time you want to know your method name the following :

    Imports System.Reflection

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            MsgBox(MethodInfo.GetCurrentMethod.Name)
            MySub()
        End Sub
    ......
    End Class

    This is not much longer and gives the same result.
    Hope it helps

    Glom
    0
     
    LVL 5

    Expert Comment

    by:Ignacio Soler Garcia
    You can do it in this way. It's excelent that I found the way to do this to get the points but I didn't when I needed this ... humans are very strange .... :^?

        Private Function GetCurrentMethodName() As String
            Dim sf As New StackFrame(2, True)
            Dim st As New StackTrace(sf)

            return st.toString()
        End Function

    Maybe you will need some string formatting to let the string like you want ....

    Hope this help. ask for more if you need ...

    SoMoS
    0
     
    LVL 5

    Expert Comment

    by:Ignacio Soler Garcia
    Or this other that gives you the string better formatted:

        Private Function GetCurrentMethodName() As String
            Dim strace As New StackTrace
            Dim frame As New StackFrame
            frame = strace.GetFrame(1)

            return frame.GetMethod().Name()
        End Function

    0
     
    LVL 5

    Assisted Solution

    by:Ignacio Soler Garcia
    And finally, the shortest one :)

        Private Function GetCurrentMethodName() As String
            return New StackTrace().GetFrame(1).GetMethod.Name
        End Function

    It's always best to think first and write after this but It's hard for me ... :D
    0
     
    LVL 4

    Expert Comment

    by:Glom
    Wow, good job SoMoS !
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Suggested Solutions

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now