473,320 Members | 2,117 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.

C++ Friendship in C#

Hi,

I would like class B to access private members of class A
for optimisation reasons... To be specific. B instanciates A
and should initializes A's members to some values. But the public
interface of A shouldn't expose A's member variables.

Is there a way to do this in C#?

I've already done this in C++!

Thanks!

Mike

May 2 '07 #1
5 1605
Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")]
it just give visibility to your assembly

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

MHi,
M>
MI would like class B to access private members of class A
Mfor optimisation reasons... To be specific. B instanciates A
Mand should initializes A's members to some values. But the public
Minterface of A shouldn't expose A's member variables.
MIs there a way to do this in C#?
M>
MI've already done this in C++!
M>
MThanks!
M>
MMike
M>
May 2 '07 #2
"Michael Nemtsev" <ne*****@msn.comwrote in message
news:a2***************************@msnews.microsof t.com...
Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")] it just give
visibility to your assembly

Ok but I'm doing web development, I guess that my classes
would be in the same assembly!?

Thanks for your prompt reply!

Mike

May 2 '07 #3
Hello Mike Gleason jr Couturier,

Then use internal or internal protected type visibility

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

M"Michael Nemtsev" <ne*****@msn.comwrote in message
Mnews:a2***************************@msnews.microso ft.com...
M>
>Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")] it just
give visibility to your assembly
MOk but I'm doing web development, I guess that my classes would be in
Mthe same assembly!?
M>
MThanks for your prompt reply!
M>
MMike
M>
May 2 '07 #4
Hi Mike,

In C# you can't have friendship like relations between classes. What you can
do, is to use internal access modifier(instead of public) and then all the
classes that reside in the same assembly will have access to those members
as if they were declared public, and classes that are in other assemblies
will not see those members (as if they were declared private). If you want
to extend this internal behaviour to another assembly, then you can use the
InternalsVisibleToAttribute
(http://msdn2.microsoft.com/en-us/lib...k(VS.80).aspx). So by using
this attribute, you don't need to have your classes in the same assembly.

Regards,

Tiberiu Covaci
MCT, MCPD

"Mike Gleason jr Couturier" <mc**************@bmgmultimedia.comwrote in
message news:83**********************************@microsof t.com...
"Michael Nemtsev" <ne*****@msn.comwrote in message
news:a2***************************@msnews.microsof t.com...
>Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")] it just give
visibility to your assembly


Ok but I'm doing web development, I guess that my classes
would be in the same assembly!?

Thanks for your prompt reply!

Mike
May 2 '07 #5
Thank you for both of you !!
May 2 '07 #6

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

Similar topics

4
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
3
by: Dave | last post by:
Hello all, I need an idiom to do the following: I have a set of non-public member functions in my class. I would like to grant access to *some* of them to another class. I can't just declare...
7
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip...
4
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
3
by: tom.s.smith | last post by:
I know that friendship isn't inherited in C++, and have two questions. (1) Why? Is it a technical problem, or just by design? If the latter, isn't this a little prescriptive, and shouldn't there...
2
by: Mark P | last post by:
Is there a way to do anything like the following: class A { friend void B::foo(); ... }; class B
4
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes...
6
by: Hicham Mouline | last post by:
Hello, A semi-skeptical colleague is asking me why friendship is not inheritable, specifically: class Base { public: virtual void f() const =0; }; class Derived : public Base {
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.