Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Friend WithEvents' in Visual Basic vs. 'private' in C#

In Windows Forms projects, why does the designer by default use the Friend WithEvents attributes in VB.NET and private ones in C#?

For example, in a form.designer. file:

.cs

private Label Label1;

.vb

Friend WithEvents Label1 as Label;

For WithEvents, it is more or less clear (for using Handles, apparently). But why Friend in Visual Basic and private in C#?

like image 555
serhio Avatar asked Dec 17 '09 15:12

serhio


People also ask

What does friend mean in Visual Basic?

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. You can use Friend only at the module, interface, or namespace level.

What is event in Visual Basic?

An event is a signal that informs an application that something important has occurred. For example, when a user clicks a control on a form, the form can raise a Click event and call a procedure that handles the event.

What is an event handler in Visual Studio?

An event handler is the code you write to respond to an event. An event handler in Visual Basic is a Sub procedure. However, you do not normally call it the same way as other Sub procedures. Instead, you identify the procedure as a handler for the event.


2 Answers

Friend is used for compatibility with older Visual Basic code, where normally a control was used outside the form which contained it.
In C# there isn't that necessity.

private is a better solution, for new code.

like image 180
apaderno Avatar answered Oct 15 '22 09:10

apaderno


Typically VB.NET leans towards exposing too much (privacy is mostly opt-in) whereas C# is the inverse, privacy is typically opt-out. As others have mentioned the reason is likely due to VB.NET's legacy and the "friendliness" of exposing everything; it makes it easy to get started but also leads to poor design and additional effort to ensure loose coupling.

like image 22
STW Avatar answered Oct 15 '22 09:10

STW