0

As answered here, the equivalent of a C# static class in VB is a module.

However, as stated on MSDN, "A module is implicitly imported into every source file in a project so the methods and properties of the module can be accessed without being fully qualified."

Is there a way to prevent this implicit import? Example:

Module moduleName
  Public Const field As String = "field"
End Module

In my classes, I want to be forced to use:

moduleName.field

Instead of just:

field

So when I read the code, I know where the "field" comes from (in C#, static classes are not implicitly imported).

Community
  • 1
  • 1
Matthieu
  • 806
  • 8
  • 12
  • 1
    If you don't want that behaviour, why don't you use a class? – Tim Schmelter Jun 12 '15 at 09:38
  • Ideally, I would like to use a "NotInheritable Shared Class", however it's not valid in VB. The other post suggests to use a Module instead, which is indeed quite similar to a static class. The only problem is this implicit import. If there are no solutions, then I will go back to a class yes, because fully qualifying the field is quite important to me. – Matthieu Jun 12 '15 at 09:53
  • 2
    You could implement a `NotInheritable Class` and make all members shared. – Tim Schmelter Jun 12 '15 at 09:56
  • @sloth: thanks, this answers my question indeed, I'll mark mine as a duplicate. – Matthieu Jun 12 '15 at 11:45
  • @TimSchmelter: as it is not possible to prevent the "implicit import", I'll do as you suggest, and use a "NotInheritable Class", even though it is not 100% the same as in C#. Thanks! – Matthieu Jun 12 '15 at 11:49
  • 1
    @Matthieu Funny enough, C# 6 added the possibility to invoke a static method without explicitly referencing the type... – sloth Jun 12 '15 at 13:10
  • @sloth You're right!!! I didn't know that. I just saw an example, how horrible the code looks like in my opinion:-) – Matthieu Jun 12 '15 at 13:39
  • @Matthieu Well, as with every feature, you can do good or you can do bad :-) – sloth Jun 12 '15 at 13:51

0 Answers0