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#?
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.
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.
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.
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.
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.
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