473,461 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ type friend functionality in C#

I keep running into places in my code where I would like only a particular
class of object to be able to access certain members of another class of
object. In C++ the friend keyword provided this functionality. The only
thing I have found in C# is internal but this is still broader in scope than
I am looking for.

Is there a way in C# to get the functionality of the C++ friend keyword?

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]
Nov 17 '05 #1
4 1189
Howard,

There is no way to do this in C#, internal is the best you could do in
this situation. You might be able to get around this by dividing up your
assembly into other assemblies, but this could prove to be cumbersome.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Howard Swope" <howardsnewsATspitzincDOTcom> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I keep running into places in my code where I would like only a particular
class of object to be able to access certain members of another class of
object. In C++ the friend keyword provided this functionality. The only
thing I have found in C# is internal but this is still broader in scope
than I am looking for.

Is there a way in C# to get the functionality of the C++ friend keyword?

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]

Nov 17 '05 #2
Sometimes it helps to use nested classes. The inner classes can access the
outer class's private members but not vice versa.
"Howard Swope" <howardsnewsATspitzincDOTcom> schrieb im Newsbeitrag
news:#T**************@TK2MSFTNGP10.phx.gbl...
I keep running into places in my code where I would like only a particular
class of object to be able to access certain members of another class of
object. In C++ the friend keyword provided this functionality. The only
thing I have found in C# is internal but this is still broader in scope than I am looking for.

Is there a way in C# to get the functionality of the C++ friend keyword?

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]

Nov 17 '05 #3
Yes, that would do the trick. Do you know of anyway to define the class as
nested but still have the code in another file (just to keep things clean)?
Some type of forward declaration or something like that?

"cody" <de********@gmx.de> wrote in message
news:ee****************@TK2MSFTNGP10.phx.gbl...
Sometimes it helps to use nested classes. The inner classes can access the
outer class's private members but not vice versa.
"Howard Swope" <howardsnewsATspitzincDOTcom> schrieb im Newsbeitrag
news:#T**************@TK2MSFTNGP10.phx.gbl...
I keep running into places in my code where I would like only a
particular
class of object to be able to access certain members of another class of
object. In C++ the friend keyword provided this functionality. The only
thing I have found in C# is internal but this is still broader in scope

than
I am looking for.

Is there a way in C# to get the functionality of the C++ friend keyword?

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]


Nov 17 '05 #4
I fear this is currently not possible.
In .NET 2.0 you have the possibility to split up classes into multiple
source files.
"Howard Swope" <howardsnewsATspitzincDOTcom> schrieb im Newsbeitrag
news:ui**************@TK2MSFTNGP10.phx.gbl...
Yes, that would do the trick. Do you know of anyway to define the class as
nested but still have the code in another file (just to keep things clean)? Some type of forward declaration or something like that?

"cody" <de********@gmx.de> wrote in message
news:ee****************@TK2MSFTNGP10.phx.gbl...
Sometimes it helps to use nested classes. The inner classes can access the outer class's private members but not vice versa.
"Howard Swope" <howardsnewsATspitzincDOTcom> schrieb im Newsbeitrag
news:#T**************@TK2MSFTNGP10.phx.gbl...
I keep running into places in my code where I would like only a
particular
class of object to be able to access certain members of another class of object. In C++ the friend keyword provided this functionality. The only
thing I have found in C# is internal but this is still broader in scope

than
I am looking for.

Is there a way in C# to get the functionality of the C++ friend keyword?
--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]



Nov 17 '05 #5

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

Similar topics

2
by: mosfet | last post by:
Hi I would like to write a template method, something like : template <typename Type> int MyClass::_Foo(vector<Type> MyVec) { if (int type) ..... if (long type)
2
by: Ruben Lysens | last post by:
Hi, Consider the example below. A Device controls a number of Ports. I'd like the Device to be in charge of creating/deleting Ports. I enforce this by making the Ports constructor and destructor...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
7
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
2
by: anongroupaccount | last post by:
I have a smaller class that essentially acts as a record for a larger class (stored in a vector inside of the larger class). In a sense, it's splitting functionality over two classes. I don't...
10
by: cody | last post by:
Hi! I have a class Customer which contains Contacts in a ContactsCollection. class Customer { ... public ContactsCollection Contacts {
2
by: Dave Rudolf | last post by:
Hey all, So I have a template class whose only template parameter is a type that the class is to manage. It's actually an implementation of the common Singleton design pattern. The class looks...
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.