468,253 Members | 1,296 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

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 2438
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Neil Zanella | last post: by
30 posts views Thread by Neil Zanella | last post: by
108 posts views Thread by Bryan Olson | last post: by
2 posts views Thread by Jon | last post: by
2 posts views Thread by Francois Malgreve | last post: by
15 posts views Thread by Jason | last post: by
2 posts views Thread by karinmorena | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.