Friend Class

P

Paul Cheetham

Hi,

I am writing an application with a large number of various classes, and
I want some of them to have Friend access to protected members of other
classes.
i.e. I want class A to have access to internal members of Class B
In C++ this was simple, as in the definition for class B, I would
declare calss A as a friend.

Seeing that C# had a friend keyword I thought this was going to be easy,
only to discover, that in typical Microsoft style, the friend keyword
has been changed to mean something completely different!

The only option I have come up with so far is to make public anything I
need to access from another class, but this doesn't really solve my
problem, as I want to make sure these functions can only be accessed by
the classes I specify.

Can anyone tell me how I can achieve the desired results please?


Thankyou in advance.

Paul Cheetham.
 
N

Nicholas Paldino [.NET/C# MVP]

Paul,

The closest thing you can do is use the internal keyword, and keep the
classes in the same assembly.

Hope this helps.
 
B

Bill Butler

Paul Cheetham said:
Hi,

I am writing an application with a large number of various classes, and I want some of them to
have Friend access to protected members of other classes.
i.e. I want class A to have access to internal members of Class B
In C++ this was simple, as in the definition for class B, I would declare calss A as a friend.

Seeing that C# had a friend keyword I thought this was going to be easy, only to discover, that in
typical Microsoft style, the friend keyword has been changed to mean something completely
different!

The only option I have come up with so far is to make public anything I need to access from
another class, but this doesn't really solve my problem, as I want to make sure these functions
can only be accessed by the classes I specify.

Can anyone tell me how I can achieve the desired results please?

Check out the "internal" access modifier.
It's not exactly the same, but you can achieve similar results

Bill
 
P

Paul Cheetham

Nicholas said:
Paul,

The closest thing you can do is use the internal keyword, and keep the
classes in the same assembly.

Hope this helps.

Thankyou.

Unfortunately it doesn't help as all my classes need to be available
from outside the assembly.

I just can't believe that MS have destroyed such a useful type modifier!



Thanks.

Paul
 
N

Nicholas Paldino [.NET/C# MVP]

I think it is a matter of opinion whether or not it is useful. If I had
to hazard a guess, the thinking at MS would be that types that are so
closely related should be packaged together.
 
B

Bill Butler

Paul Cheetham said:
Thankyou.

Unfortunately it doesn't help as all my classes need to be available from outside the assembly.

You misunderstand.
The class can be public with SOME methods declared internal
Use these internal methods for 'friend' behavior
Outside of the assembly all of the public stuff will be visible


I just can't believe that MS have destroyed such a useful type modifier!

Wait till you know the facts before you bash them on this one.

Bill
 
P

Paul Cheetham

Bill said:
You misunderstand.
The class can be public with SOME methods declared internal
Use these internal methods for 'friend' behavior
Outside of the assembly all of the public stuff will be visible

OK now I get it.
Certainly not perfect, as I was wanting to hide it more than that, but a
definite improvement on public.

Thankyou.

Paul
 
C

Christoph Nahr

Seeing that C# had a friend keyword

Excuse me, but when was that? I've never seen a friend keyword in C#.
Last I checked only C++ and Visual Basic had that keyword (and the VB
variant means the same as "internal" in C#).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top