Class name for current instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.microsoft.com

    #1

    Class name for current instance

    How do I get the class name for a current instance. For example, if I want
    to know the Class Name for the current form, how do I get this
    programatically .

    Thanks


  • Stanimir Stoyanov

    #2
    RE: Class name for current instance

    Hi,

    You can use System.Reflecti on.MethodBase's [1] shared GetCurrentMetho d
    method to obtain information regarding the currently executed method. The
    DeclaringType [2] property returns the type where the method is defined.

    Sample code:

    Imports System
    Imports System.Reflecti on

    Class TestClass
    Shared Sub Main(ByVal args() as String)
    Dim t as Type = MethodBase.GetC urrentMethod(). DeclaringType
    If Not (t is Nothing) Then
    Console.WriteLi ne(t.FullName) ' Will output 'TestClass'
    End If
    End Sub
    End Class

    [1]:
    http://msdn2.microsoft.com/en-US/lib...se(VS.80).aspx
    [2]:
    http://msdn2.microsoft.com/en-us/lib...aringtype.aspx
    --
    Stanimir Stoyanov
    www.stoyanoff.info


    "news.microsoft .com" wrote:
    [color=blue]
    > How do I get the class name for a current instance. For example, if I want
    > to know the Class Name for the current form, how do I get this
    > programatically .
    >
    > Thanks
    >
    >
    >[/color]

    Comment

    • Marina Levit [MVP]

      #3
      Re: Class name for current instance

      That will get the name of type that declares the method - which could be an
      ancestor. That's not the same as the name of the type of the current
      instance.

      Also, FullName gives the fully qualified name - including namespace.

      To get just the name of the current instance:

      Dim typeName as String = Me.GetType().Na me

      "Stanimir Stoyanov" <admin{at}stoya noff{dot}info> wrote in message
      news:8A105E57-68F3-4D81-98CB-8671F8A1D993@mi crosoft.com...[color=blue]
      > Hi,
      >
      > You can use System.Reflecti on.MethodBase's [1] shared GetCurrentMetho d
      > method to obtain information regarding the currently executed method. The
      > DeclaringType [2] property returns the type where the method is defined.
      >
      > Sample code:
      >
      > Imports System
      > Imports System.Reflecti on
      >
      > Class TestClass
      > Shared Sub Main(ByVal args() as String)
      > Dim t as Type = MethodBase.GetC urrentMethod(). DeclaringType
      > If Not (t is Nothing) Then
      > Console.WriteLi ne(t.FullName) ' Will output 'TestClass'
      > End If
      > End Sub
      > End Class
      >
      > [1]:
      > http://msdn2.microsoft.com/en-US/lib...se(VS.80).aspx
      > [2]:
      > http://msdn2.microsoft.com/en-us/lib...aringtype.aspx
      > --
      > Stanimir Stoyanov
      > www.stoyanoff.info
      >
      >
      > "news.microsoft .com" wrote:
      >[color=green]
      >> How do I get the class name for a current instance. For example, if I
      >> want
      >> to know the Class Name for the current form, how do I get this
      >> programatically .
      >>
      >> Thanks
      >>
      >>
      >>[/color][/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Class name for current instance

        Marina,

        "Marina Levit [MVP]" <someone@nospam .com> schrieb:[color=blue]
        > That will get the name of type that declares the method - which could be
        > an ancestor. That's not the same as the name of the type of the current
        > instance.[/color]

        That's true. However, it makes sense when dealing with shared members as
        'Me.GetType()' doesn't work inside these methods :-).

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Peter Macej

          #5
          Re: Class name for current instance

          What about TypeName function?

          --
          Peter Macej
          Helixoft - http://www.vbdocman.com
          VBdocman - Automatic generator of technical documentation for VB, VB
          ..NET and ASP .NET code

          Comment

          • Marina Levit [MVP]

            #6
            Re: Class name for current instance

            Right, the question did specifically ask how to get the name of the 'current
            instance', so that implied there was an instance in question. So I just
            responded to that.

            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
            news:ehPYDxDeGH A.3484@TK2MSFTN GP04.phx.gbl...[color=blue]
            > Marina,
            >
            > "Marina Levit [MVP]" <someone@nospam .com> schrieb:[color=green]
            >> That will get the name of type that declares the method - which could be
            >> an ancestor. That's not the same as the name of the type of the current
            >> instance.[/color]
            >
            > That's true. However, it makes sense when dealing with shared members as
            > 'Me.GetType()' doesn't work inside these methods :-).
            >
            > --
            > M S Herfried K. Wagner
            > M V P <URL:http://dotnet.mvps.org/>
            > V B <URL:http://classicvb.org/petition/>[/color]


            Comment

            • news.microsoft.com

              #7
              Re: Class name for current instance

              Thank you all very much for this information, it has helped me progress with
              what I was doing.

              It's nice to see that people can be so helpful.




              "Marina Levit [MVP]" <someone@nospam .com> wrote in message
              news:%238$5awEe GHA.4720@TK2MSF TNGP03.phx.gbl. ..[color=blue]
              > Right, the question did specifically ask how to get the name of the
              > 'current instance', so that implied there was an instance in question. So
              > I just responded to that.
              >
              > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
              > news:ehPYDxDeGH A.3484@TK2MSFTN GP04.phx.gbl...[color=green]
              >> Marina,
              >>
              >> "Marina Levit [MVP]" <someone@nospam .com> schrieb:[color=darkred]
              >>> That will get the name of type that declares the method - which could be
              >>> an ancestor. That's not the same as the name of the type of the current
              >>> instance.[/color]
              >>
              >> That's true. However, it makes sense when dealing with shared members as
              >> 'Me.GetType()' doesn't work inside these methods :-).
              >>
              >> --
              >> M S Herfried K. Wagner
              >> M V P <URL:http://dotnet.mvps.org/>
              >> V B <URL:http://classicvb.org/petition/>[/color]
              >
              >[/color]


              Comment

              • news.microsoft.com

                #8
                Re: Class name for current instance

                The suggested reflection method seems to do the trick nicely

                Thanks


                "Peter Macej" <peter@vbdocman .com> wrote in message
                news:udk8LXEeGH A.1856@TK2MSFTN GP03.phx.gbl...[color=blue]
                > What about TypeName function?
                >
                > --
                > Peter Macej
                > Helixoft - http://www.vbdocman.com
                > VBdocman - Automatic generator of technical documentation for VB, VB .NET
                > and ASP .NET code[/color]


                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Class name for current instance

                  "Marina Levit [MVP]" <someone@nospam .com> schrieb:[color=blue]
                  > Right, the question did specifically ask how to get the name of the
                  > 'current instance', so that implied there was an instance in question. So
                  > I just responded to that.[/color]

                  There is nothing wrong with your reply! I simply wanted to make the OP
                  aware that there are certain cases where the method described by Stanimir
                  makes sense.

                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://classicvb.org/petition/>

                  Comment

                  Working...