-
Inherited method that calls new static method
In C#, is it possible to call a new static method from an inherited method? When I try to do this, it always calls the "hidden" static method in the base class.
eg, in the example below, when I call DoSomething() on an instance of Child, I want it to call the static method Child.DoSomethingElse() but when I implement this, it calls the static method Parent.DoSomethingElse() only. I am using the new keyword since you can't override a static method.
Code:
public class Parent
{
public static void DoSomethingElse() { }
public void DoSomething()
{
DoSomethingElse();
}
}
public class Child : Parent
{
public new static void DoSomethingElse() { }
}
-
No, that's not possible because "new" breaks the inheritance chain, so DoSomething in the base class knows nothing about your new DoSomethingElse method. The only solution I can think of is to add a new DoSomething method in the Child class.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Thanks. Regarding adding DoSomething() in the Child class, yes, that does work, although I then have to add the DoSomething() method to every derived class (I have several that inherit from Parent). I have resorted to making the DoSomethingElse() not static, using abstract in the Parent and override in the Child.
I realize that on the surface, one would wonder why I would want the DoSomethingElse() to be static if I need to create an instance to call DoSomething() anyway. The reason is that sometimes I need to call DoSomethingElse() independently and don't want to have to create an instance to do so.
Similar Threads
-
Replies: 6
Last Post: 07-17-2006, 05:31 AM
-
By lordanki in forum Java
Replies: 3
Last Post: 04-04-2006, 10:13 AM
-
Replies: 3
Last Post: 01-22-2006, 08:27 AM
-
Replies: 2
Last Post: 06-22-2005, 08:21 AM
-
By Sean Woods in forum .NET
Replies: 2
Last Post: 02-20-2003, 05:23 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks