Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET class members public by default

Tags:

vb.net

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?

like image 248
Patrick J Collins Avatar asked Feb 06 '13 08:02

Patrick J Collins


People also ask

Are class members private by default?

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.

What is default visibility of class member?

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.

What is default access modifier in VB net?

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.

What is public class in VB net?

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.


1 Answers

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?

like image 175
Tim Schmelter Avatar answered Sep 30 '22 19:09

Tim Schmelter