98

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.

Community
  • 1
  • 1
MagicKat
  • 9,695
  • 6
  • 32
  • 43

5 Answers5

135

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    yes a module is a static class. Just read up on extension methods and MS will tell you the same thing. – chrissie1 Sep 25 '08 at 21:19
  • Interesting. I haven't played with extensions in VB. – MagicKat Sep 25 '08 at 22:21
  • 2
    I retract my previous comment. I was looking at a module in reflector and the IL shows it as sealed. – MagicKat Sep 25 '08 at 22:30
  • Is there a way to treat the module name as a simple class (e.g. it shouldn't be like a namespace)? – Shimmy Weitzhandler Feb 07 '10 at 23:09
  • @Shimmy: I'm not sure what you're asking - what is it about namespaces that module names emulate you don't like, and how would you expect it to be different with a class? – Joel Coehoorn Feb 07 '10 at 23:22
  • 30
    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. – Shimmy Weitzhandler Feb 08 '10 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. – Joel Coehoorn Jul 23 '13 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 '14 at 21:18
  • 23
    @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 08 '14 at 20:10
  • 2
    Yet another way that `Module` is not the same as a static `Class` is that generic `Module` declarations are not allowed. It's unfortunate that the language doesn't have a way to enforce true static classes, but it's pretty clear the actual equivalent would be as described in [the other answer](http://stackoverflow.com/a/18922541/3538012) – Peter Duniho Feb 24 '17 at 20:01
63

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

Community
  • 1
  • 1
Gary Newsom
  • 639
  • 5
  • 2
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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Charles Graham
  • 24,293
  • 14
  • 43
  • 56
  • 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. – Shimmy Weitzhandler Feb 07 '10 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

Mentor
  • 101
  • 1
  • 8
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.

Ilya Ryzhenkov
  • 11,782
  • 1
  • 40
  • 50