views:

14

answers:

2

Is it possible to prohibit an instance of a class from calling a shared/static method?

For example:

I want to allow this:

ClassName.MethodOne()

But I want to disallow this:

Dim A As New ClassName
A.MethodOne()

The reason this is desirable is that in this case it is semantically confusing if an instance can call the method.

+2  A: 

No this is not possible in VB.Net. The above code will emit a warning (BC42025) but will sucessfully compile. The only hard option is to switch warnings to errors and that will prevent this problem.

JaredPar
+1  A: 

As it was said it is warning by default. But it is possible to promote it to error. Go to properties of VB.net project, then Compile tab, then Warning configurations. Find there "Instance variable accesses shared member" and set it to Error. Now it will not compile.

Andrey