Prohibit an Instance from calling Shared/Static Method?
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.
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.
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.
精彩评论