tags:

views:

5441

answers:

4

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.

+11  A: 

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
yes a module is a static class. Just read up on extension methods and MS will tell you the same thing.
chrissie1
Interesting. I haven't played with extensions in VB.
MagicKat
I retract my previous comment. I was looking at a module in reflector and the IL shows it as sealed.
MagicKat
Is there a way to treat the module name as a simple class (e.g. it shouldn't be like a namespace)?
Shimmy
@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
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
A: 

I think the VB.NET equivalent of static is Shared.

hangy
yes ... but, you can't mark a class Shared.
MagicKat
You just mark it 'NotInheritable' and declare all it's members as Shared; you would have to do this in C# as well.
Shimmy
+3  A: 

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.

Charles Graham
You can't mark a class as shared.
Joel Coehoorn
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
A: 

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