?
Solved

How to get Class and Procedure name?

Posted on 2004-10-28
8
Medium Priority
?
175 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
Comment
Question by:KarcOrigin
  • 4
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
Glom earned 300 total points
ID: 12431683
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
ID: 12431695
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
ID: 12431828
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
Industry Leaders: 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!

 
LVL 4

Expert Comment

by:Glom
ID: 12432621
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
ID: 12442836
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
ID: 12442850
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
Ignacio Soler Garcia earned 200 total points
ID: 12442862
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
ID: 12470609
Wow, good job SoMoS !
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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 …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

578 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