473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.Reflection.MethodBase.GetCurrentMethod 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.Reflection.MethodBase
....
Public Class ctlUserMaintenance : Inherits ctlBaseEdit
....
Protected Overrides Sub InitForm()
mPageName = "User Maintenance"
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
Try
SetDirtyFlag(False)
InitGrid()
Catch ex As Exception
SendError(ex, GetCurrentMethod.Name, mPageName)
End Try
End Sub
==========================

The SendError routine displays/logs the error. As you can see
GetCurrentMethod.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 "ctlUserMaintenance" 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.
Dec 4 '07 #1
2 1455
>How can you get the current class of a method?
In an instance method such as OnLoad you can simply check
Me.GetType().Name. In a Shared method,
MethodBase.GetCurrentMethod().DeclaringType.Name.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 5 '07 #2
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" <ge*****@gmail.comwrote in message
news:30**********************************@e67g2000 hsc.googlegroups.com...
How can you get the current class of a method?

We are writting out trace error messages using the
System.Reflection.MethodBase.GetCurrentMethod 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.Reflection.MethodBase
...
Public Class ctlUserMaintenance : Inherits ctlBaseEdit
...
Protected Overrides Sub InitForm()
mPageName = "User Maintenance"
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
Try
SetDirtyFlag(False)
InitGrid()
Catch ex As Exception
SendError(ex, GetCurrentMethod.Name, mPageName)
End Try
End Sub
==========================

The SendError routine displays/logs the error. As you can see
GetCurrentMethod.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 "ctlUserMaintenance" 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.

Dec 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nayt Grochowski | last post by:
Does anyone see any problem with the loading a SqlConnection into the System.Web.HttpContextCurrent.Items collection in a Page's Constructor. Then Closing and Disposing of it the OnUnload method? ...
15
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. How can one get the name of the current project and the current class? This is the situation. Suppose there is a project called "P1".
15
by: Jason | last post by:
Currently, I am storing information about the currently logged on user in Session variables that are stored in SQL. However, I am using role-based security, so I am storing custom roles in a...
2
by: | last post by:
Today I learned that creating cookies inside of a custom class in ASP.NET 2.0 requires that you prefix it with HttpContext.Current..., e.g. : ...
0
by: Rodusa | last post by:
I am trying to replace cookies with the new "Asp.net 2.0 profiles" in my shopping cart application, but I am having trouble to access profile properties using HttpContext.Current.Profile. I can...
8
by: news.microsoft.com | last post by:
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
1
by: Joseph Geretz | last post by:
I have a web service page which uses WSE 2.0 SP3 to return a file attachment on one of its method calls. All web service methods are functioning properly, except for this one method which uses WSE...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
8
by: eric | last post by:
I have a 2.0 asp.net project. In a class contained within a seperate project, I am trying to reference HttpContext.Current.Session but Session is always null. I've tried implementing...
8
by: SpaceMarine | last post by:
hello, my web app form has many DropDownLists that pull their content from a database. these calls are in a Business Access Layer, when first checks the context's Cache object for existing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.