PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

C++ Friendship in C#

 
 
Mike Gleason jr Couturier
Guest
Posts: n/a
 
      2nd May 2007
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

 
Reply With Quote
 
 
 
 
Michael Nemtsev
Guest
Posts: n/a
 
      2nd May 2007
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

M> Hi,
M>
M> I would like class B to access private members of class A
M> for optimisation reasons... To be specific. B instanciates A
M> and should initializes A's members to some values. But the public
M> interface of A shouldn't expose A's member variables.
M> Is there a way to do this in C#?
M>
M> I've already done this in C++!
M>
M> Thanks!
M>
M> Mike
M>


 
Reply With Quote
 
Mike Gleason jr Couturier
Guest
Posts: n/a
 
      2nd May 2007
"Michael Nemtsev" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
Michael Nemtsev
Guest
Posts: n/a
 
      2nd May 2007
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" <(E-Mail Removed)> wrote in message
M> news:(E-Mail Removed)...
M>
>> Hello Mike Gleason jr Couturier,
>>
>> Look at [assembly: InternalsVisibleTo("MyFriendAssembly")] it just
>> give visibility to your assembly
>>

M> Ok but I'm doing web development, I guess that my classes would be in
M> the same assembly!?
M>
M> Thanks for your prompt reply!
M>
M> Mike
M>


 
Reply With Quote
 
Tiberiu Covaci
Guest
Posts: n/a
 
      2nd May 2007
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/library/0tke9fxk(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" <(E-Mail Removed)> wrote in
message news:83BD1790-C729-4868-88D3-(E-Mail Removed)...
> "Michael Nemtsev" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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


 
Reply With Quote
 
Mike Gleason jr Couturier
Guest
Posts: n/a
 
      2nd May 2007
Thank you for both of you !!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friendship bittoo Windows XP Customization 4 10th Feb 2005 01:33 AM
Re: Looking for friendship YanquiDawg Computer Hardware 0 5th Nov 2004 12:26 AM
friendship =?Utf-8?B?bWFyaXRvbGU=?= Microsoft Access 1 15th Oct 2004 07:34 PM
friendship =?Utf-8?B?bWFyaXRvbGU=?= Microsoft Access 1 15th Oct 2004 03:59 AM
friendship ricky Windows XP General 1 27th Nov 2003 09:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:38 AM.