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

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

For example, in the form.designer. file form.designer. :

.cs

 private Label Label1; 

.vb

 Friend WithEvents Label1 as Label; 

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

+7
c # field winforms
source share
4 answers

Friend WithEvents used for compatibility with old Visual Basic code, where the control is usually used outside the form that contained it.
In other words, C # is not necessary.

private is the best solution for new code.

+7
source share

As a rule, VB.NET relies on excessive exposure (confidentiality is mostly up to you), while C # is inverse, privacy is usually turned off. As others have said, the reason is probably related to the legacy of VB.NET and the "friendliness" of exposing everything; this makes it easy to get started, but also leads to poor design and additional efforts to ensure free grip.

+4
source share

I think this will help with the migration from earlier versions of VB, as the code in forms most often changes externally. Friend is also the default.

Private is better in terms of code design and is used in C # because, I believe, there is no similar historical coding practice.

+2
source share

Perhaps Visual Basic is trying to be friendlier to new programmers and allowing them to see all the Friend controls in the form from anywhere in their project?

A friend who is internal, the only thing I can think of, I'm not sure why the default values ​​will be different from others than to help new programmers ...

0
source share

All Articles