I have a base class with several derived classes. I want all of my derived classes to have the same Public Shared
(static
) method with their own implementation. How do I do this? Is it even possible?
-
As noted by SO down the side of this question there are many similar questions, including this one http://stackoverflow.com/questions/1380087/whats-the-correct-alternative-to-static-method-inheritance-c which has some further relevant answers. – Mark Hurd Mar 18 '10 at 15:07
3 Answers
I know this seems like a useful feature "at design time" but imagine how this could be used:
If GetForms
is Shared
(static
in C#) you don't have an object with a type to distinguish which method to use, i.e. you can't say BaseForm.GetForms()
in some way that it can be determined which of ChildFormTypeA.GetForms()
and ChildFormTypeB.GetForms()
is actually called.
The best you can do is document this is the intention if you just want explicit calls to ChildFormTypeA.GetForms()
and ChildFormTypeB.GetForms()
, or if you really need the concept make it a factory method e.g.
MustInherit Class BaseForm
MustOverride Sub GetForms()
End Class
Class MyForm
Inherits BaseForm
Public Sub New()
End Sub
Overrides Sub GetForms()
End Sub
End Class
Now you just need
Dim f as BaseForm = New MyForm
f.GetForms
Alternatively GetForms
might be Protected
and called from the Sub New
.
EDIT: This is almost a new answer.
I have just needed this when maintaining existing code. I decided the simplest way to go was to make both classes implement an interface:
Interface ISharedNew
Function SharedNew(ByVal Argument As String) As ISharedNew
End Interface
(In this case, I have an existing use of the classes via a generic constraint T As New
and I needed the constructor to now have a parameter which generic constraints do not support.)
The new (simplified) generic code is now:
Function GetNew(Of T As {New, ISharedNew})(ByVal Argument As String) As T
Return DirectCast((New T).SharedNew(Argument), T)
End Function
In my case, the New T
object itself is ignored by the SharedNew
, but its type does specify the relevant instance of SharedNew
, which creates a new T
object using the Argument
.
The code has been added to this answer from scratch, without referencing the actual code.

- 10,665
- 10
- 68
- 101
-
Totally Agree. MyForm.GetForms() can operate only on the type MyForm and there is no way to specify the derived type form which need to be used. – Ramesh Mar 18 '10 at 13:14
-
It would be easy to distinguish, I would think, if I call something like `Invoice.GetForms()`, it would go to the shared 'GetForms()` method in the `Invoice` class... how is that difficult? In the base class, declare it as `Public MustOverride Shared Sub GetForms()`. Unfortunately, this won't compile... makes perfect sense to me that it should though... – Jason Mar 18 '10 at 17:46
-
@Jason, That's what I mean by it might as well just be documented as the intention: how is the code broken if the intended functionality was actually implemented by InvoiceOther.TheFormsGet(). – Mark Hurd Apr 07 '10 at 02:55
No. You could use a singleton, but there may be a better design.

- 278,309
- 50
- 514
- 539
By definition, a static method cannot be declared as virtual. To achieve this without static methods you would do something like the following:
public abstract class BaseFoo
{
protected virtual void Bar()
{
}
}
public class Foo : BaseFoo
{
protected override void Bar()
{
base.Bar();
}
}
If you wanted to achieve something similar to having it as static you could use the singleton pattern so that you share a single instance of the object.
Edit:
I realized your original question talked about VB syntax, so here's the same thing in VB:
Public MustInherit Class BaseFoo
Protected Overridable Sub Bar()
End Sub
End Class
Public Class Foo
Inherits BaseFoo
Protected Overrides Sub Bar()
MyBase.Bar()
End Sub
End Class

- 10,009
- 2
- 29
- 35
-
is there any reason for this? it seems stupid that you can't declare a static method sig that can be used across all classes. – Jason Mar 04 '10 at 04:40
-
RE: your edit - yep, thanks... i was able to convert it in my head, but yeah i'm working in vb for this project :) – Jason Mar 04 '10 at 04:44
-
What are you trying to achieve by doing this? A virtual method relies on an instantiated object, whereas a static method exists without needing the object to be created. That's why static methods can only reference other static variables/methods. If it's simply the method signature you're trying to share, the example I gave before shows that the signature would get shared. Without knowing your exact scenario here, I'd say you'd probably benefit from either the singleton pattern or dependency injection. – Greg Shackles Mar 04 '10 at 04:53
-
What I'm saying is that they do the same thing, but their implementation details require something slightly different. It would be perfect if I could declare `BaseForm.GetForms()` as `MustOverride` and just include the implementation details there, but I can't. – Jason Mar 04 '10 at 05:17