473,320 Members | 1,879 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,320 software developers and data experts.

Find current class from within a static method

Hi all,

Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is inherited.
I'd like to change the behavior in the derived class but static methods
cannot be overridden. If I can find out 'who i am' i can do what I need to
do.

thanks,

john
Sep 3 '07 #1
8 2614
John Mott <jo********@hotmail.comwrote:
Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is inherited.
I'd like to change the behavior in the derived class but static methods
cannot be overridden. If I can find out 'who i am' i can do what I need to
do.
I'm afraid not - there's no real idea of the "current class" in the
context of a static method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 3 '07 #2
Is the following similar to what you wish to achieve, such that
Animal.LegCount and Lion.LegCount return differing logic?

public class Animal
{
public static int LegCount()
{
return 0;
}
}

public class Lion : Animal
{
public new static int LegCount()
{
return 4;
}
}
"John Mott" wrote:
Hi all,

Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is inherited.
I'd like to change the behavior in the derived class but static methods
cannot be overridden. If I can find out 'who i am' i can do what I need to
do.

thanks,

john
Sep 3 '07 #3

That is defintely an improvement over what I have now. I have multiple
static routines but this scheme could reduce the number requires in the
derived class to just what it has to 'know', then it can manually call the
base class routines.

thanks!

john

"ModelBuilder" <Mo**********@discussions.microsoft.comwrote in message
news:0C**********************************@microsof t.com...
Is the following similar to what you wish to achieve, such that
Animal.LegCount and Lion.LegCount return differing logic?

public class Animal
{
public static int LegCount()
{
return 0;
}
}

public class Lion : Animal
{
public new static int LegCount()
{
return 4;
}
}
"John Mott" wrote:
>Hi all,

Is there a way to get the name of the current class in a static method? I
have a base class that contains static methods and that class is
inherited.
I'd like to change the behavior in the derived class but static methods
cannot be overridden. If I can find out 'who i am' i can do what I need
to
do.

thanks,

john

Sep 3 '07 #4
I would suggest going away from static methods in this case since what you're looking for is polymorphic behavior. I would make the method an instance method and mark it as virtual in the base class and override it in the subclass.

Carlos S
Sep 3 '07 #5
I'm with you on the behavior and many of the current methods are virtual and
overridden for the instantiated objects, but its syntactically and logically
convenient to use static methods -- they retrieve instances of the class
through a query, so they are operating like a factory:

DataTable dt = Prospects.Retrieve("state = 'TN'"); // retrieve all
Prospects where the State is Tennessee

Right now the static methods are declared on the derived classes, but I can
push these into the base class and just pass in minor information. The goal
is tidyness, more logic in the common base class and less in the derived
class.

john
<Carlos Swrote in message news:up*************@TK2MSFTNGP06.phx.gbl...
>I would suggest going away from static methods in this case since what
you're looking for is polymorphic behavior. I would make the method an
instance method and mark it as virtual in the base class and override it in
the subclass.

Carlos S

Sep 3 '07 #6
Hi John,
Right now the static methods are declared on the derived classes, but I
can push these into the base class and just pass in minor information. The
goal is tidyness, more logic in the common base class and less in the
derived class.
Have you looked at custom attributes? A very neat way of adding static meta
data to a class. Then you just need to pass the type into the base class
static methods and they get the rest from the custom attributes.

Cheers
Doug Forster
Sep 4 '07 #7
[1] StackTrace reference is available at
http://msdn2.microsoft.com/en-us/lib...tacktrace.aspx.

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org

Sep 4 '07 #8
wow. That looks promising. Thanks!

john

"Doug Forster" <nobody@nowhere,comwrote in message
news:ey**************@TK2MSFTNGP04.phx.gbl...
Hi John,
>Right now the static methods are declared on the derived classes, but I
can push these into the base class and just pass in minor information.
The goal is tidyness, more logic in the common base class and less in the
derived class.

Have you looked at custom attributes? A very neat way of adding static
meta data to a class. Then you just need to pass the type into the base
class static methods and they get the rest from the custom attributes.

Cheers
Doug Forster

Sep 4 '07 #9

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

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
2
by: Jon | last post by:
I am writing an MDI app that uses a document manager class to keep track of opened child windows. I want the user to be able to close a child window, but then re-open the window from the "Window"...
2
by: Francois Malgreve | last post by:
hello guys, I have some helper class in my ASP.NET pplication who basically contains static methods. I used them as helper methods to do small jobs that I can use at many places in my code. ...
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...
10
by: AC [MVP MCMS] | last post by:
How do you get the current instance of an executing assembly from within a static method? I'm trying to use the LicenseManager.Validate(type,instance) method and having trouble calling it from...
3
by: DrJazz | last post by:
Consider the following (which may be bad architecturally speaking, but please humor me): public class DomainObject { public static void DeleteByID(int id) { // Type currentType = ????;...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.