By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,889 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,889 IT Pros & Developers. It's quick & easy.

Friend WithEvents

P: n/a
Hi,

What does it mean when VS automatically adds a line like this to code
of my form:

Friend WithEvents Label1 As System.Windows.Forms.Label

Also, I noticed a modifier property for each control and the default is
Friend. When modifier is set to Friend, I can access the control from
anywhere with the application, while if it is set to private it can
only be accessed within the form. If I don't need to access a control
outside of a form, should I set the modifier to private? On the other
hand, if friend is the default, maybe I should leave it as is (?).

Thanks,
Aaron

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2005-03-26, ha***********@hotmail.com <ha***********@hotmail.com> wrote:
Hi,

What does it mean when VS automatically adds a line like this to code
of my form:

Friend WithEvents Label1 As System.Windows.Forms.Label
Basically, this means that Label1 is a variable representing a
Forms.Label object, the variable is visible throughout the Assembly, and
it is registered for declarative events (the "FunctionName Handles
Label1.Click" kinda stuff).

Also, I noticed a modifier property for each control and the default is
Friend. When modifier is set to Friend, I can access the control from
anywhere with the application, while if it is set to private it can
only be accessed within the form.
To be more precise, Friend properties can be accessed from anywhere in
the Assembly. This will matter when you start having multi-assembly
applications.
If I don't need to access a control
outside of a form, should I set the modifier to private? On the other
hand, if friend is the default, maybe I should leave it as is (?).


Private is better. I presume that Friend is the default because lots of
VB Classic code (and VB Classic programmers) manipulated controls from
outside the form, so MS needed that for ease of migration.

In practice though, I must admit I seldom bother to change the default,
even though I probably should.

BTW, you don't have to edit the code to do this, there's a "modifiers"
property in the properties window for this.

Nov 21 '05 #2

P: n/a
Thank you, David.
Private is better. I presume that Friend is the default because lots of VB Classic code (and VB Classic programmers) manipulated controls from outside the form, so MS needed that for ease of migration.


Would private be better to preserve abstraction between forms? In
other words, only those controls that were intended to accessed from
elsewhere in the assembly will be available.

Also, is there a memory/execution time performance benefit for private
vs friend?

Thank you,
Aaron

Nov 21 '05 #3

P: n/a
On 2005-03-27, ha***********@hotmail.com <ha***********@hotmail.com> wrote:
Thank you, David.
Private is better. I presume that Friend is the default because lots of
VB Classic code (and VB Classic programmers) manipulated controls

from
outside the form, so MS needed that for ease of migration.


Would private be better to preserve abstraction between forms?


I'd prefer to say that it preserves encapsulation within the form, but
it's the same idea I think.
In
other words, only those controls that were intended to accessed from
elsewhere in the assembly will be available.
With the caveat that it's probably never a good idea to access a form's
controls from outside the form.
Also, is there a memory/execution time performance benefit for private
vs friend?


Friend might be minutely faster, since making controls Private adds
another level of indirection to accesses, but in normal circumstances
the difference wouldn't even be measurable.

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.