473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Friend WithEvents

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
3 27201
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
2
by: Jakob Bengtsson | last post by:
Hi, I have a form (which cannot be serialized). In the form's code I declare an object like this (never mind the object nor class name, it's for illustration only): Private WithEvents...
5
by: Bradley Grant | last post by:
I am trying to write a program to access a device through USB port, there is some Visual Basic program examples that work with the device, but I am trying to write a program using C#, no luck yet,...
2
by: JJ | last post by:
Hi All I came across this piece of code in C# and was wondering what exactly does it mean to the event Friend WithEvents txtPath As System.Windows.Forms.TextBo The Friend Withevents part ...
0
by: Steve | last post by:
Hello, I have been experimenting/learning to use aspx with and without VS2003. In VS 2003 I created a simple webform/application which contains a listbox and a label. I populate the listbox...
4
by: Shapper | last post by:
Hello, I have an aspx.vb page with this code: Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Menu1 As MenuControl.Web.UI.Menu ... If I have my code in the aspx...
6
by: | last post by:
Is there any additional overhead for declaring a variable using the WithEvents keyword even if none of the events are being handled?
8
by: Bob Day | last post by:
Using VS 2003, VB.NET, MSDE If an event is raised in a class that was NOT instantiated with the WithEvents key word, is that event effectively ignored? Or, another way to phrase the question,...
8
by: DareDevil | last post by:
Class MyClas Friend ReadOnly WithEvents MyObject as AnotherClas .. End Clas I have a VB.NET class that has a readonly object (i.e. It can only be assigned in the constructor). This enables me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.