Marking A Class Static in VB.NET

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.


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
  • Shared is like method of static class.
  • NotInheritable is like sealed.
  • Private New is like static class can not be instantiated.

See:
MSDN - Static Classes and Static Class Members


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


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.

Tags:

C#

Vb.Net