473,395 Members | 1,497 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.

What is a 'friend' class?

In the context of VB.net, when/why would one use a 'friend class'. I'm
finding lots of references to friend classes on google, but not a simle
definition of what they actually are.

Actually, if there is a nice overview of the different types of classes
(public, private, shared, friend, etc.) and how they differ on a broad
level, I'd certainly like to read it. I'm at the point where I'm a 'decent
hack' and .net but I'm missing some broader overall concepts that I need to
get a better grasp on.

-Darrel
Nov 18 '05 #1
4 4289
A Friend class is only visible to other classes within the same assembly.
Here's an overview for you:
http://visualbasic.about.com/od/usin...heritancea.htm

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
In the context of VB.net, when/why would one use a 'friend class'. I'm
finding lots of references to friend classes on google, but not a simle
definition of what they actually are.

Actually, if there is a nice overview of the different types of classes
(public, private, shared, friend, etc.) and how they differ on a broad
level, I'd certainly like to read it. I'm at the point where I'm a 'decent
hack' and .net but I'm missing some broader overall concepts that I need
to
get a better grasp on.

-Darrel

Nov 18 '05 #2
One minor correction, Steve: A Friend class, method, or other entity is
visible to any classes in any assemblies within the same application, not
just within the same assembly.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
A Friend class is only visible to other classes within the same assembly.
Here's an overview for you:
http://visualbasic.about.com/od/usin...heritancea.htm

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
In the context of VB.net, when/why would one use a 'friend class'. I'm
finding lots of references to friend classes on google, but not a simle
definition of what they actually are.

Actually, if there is a nice overview of the different types of classes
(public, private, shared, friend, etc.) and how they differ on a broad
level, I'd certainly like to read it. I'm at the point where I'm a 'decent hack' and .net but I'm missing some broader overall concepts that I need
to
get a better grasp on.

-Darrel


Nov 18 '05 #3
The tutorial provided by steve is a good start, but to answer one of your
questions specifically, that is when you would use such a modifier. The C#
term is "internal" which to me is more accurate since the scope of anything
marked "friend" is only inside the assembly itself. One typical use of
this is for security purposes - your code might do something a little iffy,
which is fine as long as its confined to your own code. If your class is
public instead of friend, someone could subclass your code and make
dangerous use of your iffy code.

For example, FxCop raises the following error:
Methods used as event handlers should not be externally visible

Rule Description
Event-handling methods should not be exposed unless absolutely necessary. An
event handler that invokes the exposed method can be added to any event as
long as the handler and event signatures match. Events can potentially be
raised by any code, and are often raised by highly trusted system code in
response to user actions such as clicking a button. Adding a security check
to an event-handling method does not prevent code from registering an event
handler that invokes the method.

How to Fix Violations
To fix a violation of this rule, make the method private or internal.

As you can see, the solution is to mark such methods as private (which could
likely be too restrictive) or internal (which typically fits the bill).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
In the context of VB.net, when/why would one use a 'friend class'. I'm
finding lots of references to friend classes on google, but not a simle
definition of what they actually are.

Actually, if there is a nice overview of the different types of classes
(public, private, shared, friend, etc.) and how they differ on a broad
level, I'd certainly like to read it. I'm at the point where I'm a 'decent
hack' and .net but I'm missing some broader overall concepts that I need to get a better grasp on.

-Darrel

Nov 18 '05 #4
> > A Friend class is only visible to other classes within the same
assembly.
Here's an overview for you:
http://visualbasic.about.com/od/usin...heritancea.htm


Steve, Kevin...thank you both!

-Darrel
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
3
by: John Cho | last post by:
/* This program is a database program that will store video tape names, minutes, year released, and price I want it to be professional so that *YOU* will want to buy it and use it for a video...
1
by: Tony Johansson | last post by:
Hello experts! Assume I have this class definition of class ListElem. I'm a bit unsure how to interpret when you put friend declaration in public, protected and private section of a class...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
2
by: hsharsha | last post by:
Consider the below code: int main(void) { class inner {}; friend class inner; /* what does this signify???? */ return 0; }
12
by: TC | last post by:
I'm trying to figure out what the "Friend" keyword does. I know it specifies that "elements are accessible from within the same assembly", but that doesn't help because I don't know what an...
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
2
by: Andy Champ | last post by:
We have a class with something that, simplified, looks like this: template <typename Tclass foo { T beyondAllReaching; // In VC2005, this works OK template <typename Ufriend void...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.