开发者

Prohibit an Instance from calling Shared/Static Method?

开发者 https://www.devze.com 2023-01-13 03:58 出处:网络
Is it possible to prohibit an instance of a class from calling a shared/static method? For example: I want to allow this:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号