By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
100
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

"Dennis C. Drumm" <de*******@primacode.com> wrote in message
news:OP**************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #2

P: n/a
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
"100" <10*@100.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
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

"Dennis C. Drumm" <de*******@primacode.com> wrote in message
news:OP**************@TK2MSFTNGP12.phx.gbl...
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


Nov 15 '05 #3

P: n/a
100
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.
"Dennis C. Drumm" <de*******@primacode.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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
"100" <10*@100.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
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

"Dennis C. Drumm" <de*******@primacode.com> wrote in message
news:OP**************@TK2MSFTNGP12.phx.gbl...
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



Nov 15 '05 #4

P: n/a
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" <de*******@primacode.com>
| From: "Dennis C. Drumm" <de*******@primacode.com>
| 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: <OP**************@TK2MSFTNGP12.phx.gbl>
| 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!TK2MSFTN GP12.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
|
|
|

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.