Restricting access to a class' field to only one other class

  • Thread starter Thread starter Dennis C. Drumm
  • Start date Start date
D

Dennis C. Drumm

Is there a way with C# to allow one class access to a method or field of
another class, without making that method or field visible to all other
classes, as would be the case when making the method or field public?

Thanks,

Dennis
 
Hi Dennis,

You can make it *intrernal* or *protected internal*. This will make the
member visible only in the assembly (internal) or assembly + derived types
(protected internal).
There is no equivalent of c++'s *friend* access modifier.

However, classes decalred inside other classes have unrestricted access to
the outer class' members regardless of their access modifiers.

HTH
B\rgds
100
 
Are you refering to a nested class that has unrestricted access to the outer
class' members? I have tried that and receive a compile error "Cannot access
a nonstatic member of outer type ...". Are you sure about that? If so, how
do I access that outer class member?

Thanks,

Dennis
 
Hi Dennis,
Yes you have to have reference to an instace of the outer class.

class Outer
{
class Inner
{
private Outer mOwner;
public Inner(Outer owner)
{
mOwner = owner;
}

public void Foo()
{
mOwner.PrivateMethod(); //Can access any outer's mamber
}

}

private Inner mInner;
public Outer()
{
mInner = new Inner(this);
}

//Outer class has access to public members of mInner only

private void PrivateMethod()
{
}
}

Decalring a type nested to another doesn't inline its members into the
outer's declaration. You have to create an object of inner type.

HTH
B\rgds
100.
 
Hi Dennis,

I'd like to know if this issue is resolved. Is there anything that I can
help? I'm still monitoring on it.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Reply-To: "Dennis C. Drumm" <[email protected]>
| From: "Dennis C. Drumm" <[email protected]>
| Subject: Restricting access to a class' field to only one other class
| Date: Tue, 14 Oct 2003 14:58:29 -0400
| Lines: 9
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 206-15-139-8.dialup.ziplink.net 206.15.139.8
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:191275
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Is there a way with C# to allow one class access to a method or field of
| another class, without making that method or field visible to all other
| classes, as would be the case when making the method or field public?
|
| Thanks,
|
| Dennis
|
|
|
 
Back
Top