473,320 Members | 1,856 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,320 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 27178
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.