Reputation: 9821
As just stated in a recent question and answer, you can't inherit from a static class. How does one enforce the rules that go along with static classes inside VB.NET? Since the framework is compatible between C# and VB it would make sense that there would be a way to mark a class static, but there doesn't seem to be a way.
Upvotes: 102
Views: 117276
Reputation: 415735
Module == static class
If you just want a class that you can't inherit, use a NotInheritable
class; but it won't be static/Shared. You could mark all the methods, properties, and members as Shared
, but that's not strictly the same thing as a static class in C# since it's not enforced by the compiler.
If you really want the VB.Net equivalent to a C# static class, use a Module
. It can't be inherited and all members, properties, and methods are static/shared.
Upvotes: 140
Reputation: 659
Almost there. You've got to prevent instantiation, too.
NotInheritable Class MyStaticClass
''' <summary>
''' Prevent instantiation.
''' </summary>
Private Sub New()
End Sub
Public Shared Function MyMethod() As String
End Function
End Class
See:
MSDN - Static Classes and Static Class Members
Upvotes: 65
Reputation: 111
You can create static class in vb.net. The solution is
Friend NotInheritable Class DB
Public Shared AGE As Integer = 20
End Class
AGE variable is public static, you can use it in other code just like this
Dim myage As Integer = DB.AGE
Friend = public, NotInheritable = static
Upvotes: 4
Reputation: 24835
If you just want to create a class that you can't inherit, in C# you can use Sealed, and in VB.Net use NotInheritable.
The VB.Net equivalent of static is shared.
Upvotes: 6
Reputation: 12142
From the CLR point of view, C# static class is just "sealed" and "abstract" class. You can't create an instance, because it is abstract, and you can't inherit from it since it is sealed. The rest is just some compiler magic.
Upvotes: 2