How to get the current class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gman

    #1

    How to get the current class

    How can you get the current class of a method?

    We are writting out trace error messages using the
    System.Reflecti on.MethodBase.G etCurrentMethod to get the Current
    Method but a string is passed telling for the class. I am a newbie to
    the .NET but I would think there should be a way to say
    GetCurrentClass ?

    Here is our code.
    =============== ===========
    Imports System.Reflecti on.MethodBase
    ....
    Public Class ctlUserMaintena nce : Inherits ctlBaseEdit
    ....
    Protected Overrides Sub InitForm()
    mPageName = "User Maintenance"
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
    MyBase.OnLoad(e )
    Try
    SetDirtyFlag(Fa lse)
    InitGrid()
    Catch ex As Exception
    SendError(ex, GetCurrentMetho d.Name, mPageName)
    End Try
    End Sub
    =============== ===========

    The SendError routine displays/logs the error. As you can see
    GetCurrentMetho d.Name is used to pass "OnLoad" but mPageName is hard
    coded with "User Maintenance". I would like a way to get the Class
    name of "ctlUserMainten ance" without having to hard code it. Then I
    would be able to tell exactly what class thrown the error.

    Any other ideas on how to handle error conditions would be appretiated
    too.
    It would be really cool if I could get the SendError to open to run
    "DevEnv /Edit" and have it open the class that is causing the problem
    so I can debug.

    Thanks in advance.
  • Mattias Sjögren

    #2
    Re: How to get the current class

    >How can you get the current class of a method?
    In an instance method such as OnLoad you can simply check
    Me.GetType().Na me. In a Shared method,
    MethodBase.GetC urrentMethod(). DeclaringType.N ame.



    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Chris Mullins [MVP - C#]

      #3
      Re: How to get the current class

      The most obvious answer, "me.GetType " is probably what you're looking for.

      This may (or may not) give the right polymorphic answer, depending on what
      you want. If you're in a base class, you'll end up getting the type of the
      derived class. Sometims this is what you're looking for, sometimes not.

      .... also, this won't work inside a shared method, as there's no "me"
      parameter available.

      --
      Chris Mullins

      "gman" <geweiss@gmail. comwrote in message
      news:30557f44-dd30-44c5-b14b-6a12a159930d@e6 7g2000hsc.googl egroups.com...
      How can you get the current class of a method?
      >
      We are writting out trace error messages using the
      System.Reflecti on.MethodBase.G etCurrentMethod to get the Current
      Method but a string is passed telling for the class. I am a newbie to
      the .NET but I would think there should be a way to say
      GetCurrentClass ?
      >
      Here is our code.
      =============== ===========
      Imports System.Reflecti on.MethodBase
      ...
      Public Class ctlUserMaintena nce : Inherits ctlBaseEdit
      ...
      Protected Overrides Sub InitForm()
      mPageName = "User Maintenance"
      End Sub
      >
      Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
      MyBase.OnLoad(e )
      Try
      SetDirtyFlag(Fa lse)
      InitGrid()
      Catch ex As Exception
      SendError(ex, GetCurrentMetho d.Name, mPageName)
      End Try
      End Sub
      =============== ===========
      >
      The SendError routine displays/logs the error. As you can see
      GetCurrentMetho d.Name is used to pass "OnLoad" but mPageName is hard
      coded with "User Maintenance". I would like a way to get the Class
      name of "ctlUserMainten ance" without having to hard code it. Then I
      would be able to tell exactly what class thrown the error.
      >
      Any other ideas on how to handle error conditions would be appretiated
      too.
      It would be really cool if I could get the SendError to open to run
      "DevEnv /Edit" and have it open the class that is causing the problem
      so I can debug.
      >
      Thanks in advance.

      Comment

      Working...