[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to get Class and Procedure name?

Posted on 2004-10-28
8
Medium Priority
?
172 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
[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
  • 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
Technology Partners: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

649 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