According to MSDN:
Public access is the normal level for a programming element when you do not need to limit access to it. Note that the access level of an element declared within an interface, module, class, or structure defaults to Public if you do not declare it otherwise.
So, if I declare a class method in VB.NET without specifying an access modifier, then it is public by default:
Sub DoSomething()
End Sub
This is insane! I want members to be private by default, and only those specifically marked as Public to be visible outside the class. Like in C#... How do I modify this behaviour?
Class, record, and struct accessibility Class members, including nested classes and structs, can be public , protected internal , protected , internal , private protected , or private . Class and struct members, including nested classes and structs, have private access by default.
Default visibility allows a variable or method to be seen by all methods of a class or other classes that are part of the same package. A package is a group of related classes. For now, default visibility means about the same thing as public visibility.
Friend access is often the preferred level for an application's programming elements, and Friend is the default access level of an interface, a module, a class, or a structure.
Public Class A ' implementation of code End Class Public Class B Inherits A ' Implementation of Code End Class Public Class C Inherits A ' Implementation of code End Class. Let's create a program to understand the concept of Multi-Level Inheritance in VB.NET.
This is insane! I want members to be private by default
As Fredrik has already commented, you should always provide explicit access modifiers.
The code will be much more clear for other readers if you always explicitly include the access modifier.
I assume that this is due to downwards compatibility or developers who are not familiar with access modifiers at all.
But you are right, as in C# I would suggest to make everything as private as possible by default. You can make it more public when needed.
Declaration Contexts and Default Access Levels (VB.NET)
Any idea how to modify this behaviour?
I don't think that it's possible to specify the default access modifier somewhere in Visual Studio. You could try to create a template-class which is suggested here (not tested):
Visual C# 2010 Express: Specify default access modifier for new classes?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With