94

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.

5 Answers 5

130

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.

10
  • 2
    I retract my previous comment. I was looking at a module in reflector and the IL shows it as sealed.
    – MagicKat
    Sep 25, 2008 at 22:30
  • 29
    Are you a VBer? A module is like a namespace. All the VB functions are imported from VB's modules and they are available in the general scope. For my opinion a static class in vb is not a module, it has different behavior. if you want the 'static class' behavior in vb, you should have NotInheritable and declare all it's members 'Shared', just like you would have to do in C#'s static class, they will behave THE SAME. A module NOT. Feb 8, 2010 at 10:04
  • 2
    @Shimmy I don't know how I've missed this comment before, but you are mistaken. Look at the IL generated, and modules get the exact same treatment as static class. The VB just does extra work for the old standard functions to make them appear in the global scope. Jul 23, 2013 at 16:49
  • 2
    @JoelCoehoorn Module ≈≈ static class since a Module must be declared directly in a namespace. This means that one cannot declare a Module in a Module or Class.
    – LosManos
    Oct 16, 2014 at 21:18
  • 22
    @JoelCoehoorn The fact that modules and static classes end up with the same IL is good to know, but VB additionally promotes a module's members to the containing namespace. The promotion aspect is where modules and static classes differ in practical use and IMHO that's a massive difference. I far prefer the NotInheritable - empty private constructor - all members shared solution because members can only be reached through the class, which is far more correct behaviour if you're trying to get static-class equivalence. The IL might be different, but that's academic really.
    – misha256
    Dec 8, 2014 at 20:10
61

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

1
  • 2
    why is this not the accepted answer? Thanks for the code snip
    – ArcherBird
    Sep 18, 2018 at 17:17
6

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.

1
  • you mark it's variables and functionalities as shared, just like you would have to do in C# static class. Marking as NotInheritable makes it sealed. Feb 7, 2010 at 23:05
4

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

3
  • 6
    Friend != public, Friend = internal
    – Altair
    Jul 18, 2016 at 17:42
  • 4
    NotInheritable != static, NotInheritable == sealed
    – Magnus
    Dec 14, 2018 at 14:18
  • 4
    myage != 20 (any more, sadly)
    – Grim
    Jun 5, 2019 at 7:43
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.