473,287 Members | 1,580 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,287 software developers and data experts.

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

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
4 4596
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MoCha | last post by:
hello everyone, i want to know of any trick used to restrict the access scope of a friend class (or function). ie instead of the friend class having access to the entire internals of the class in...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
7
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
6
by: Edward Diener | last post by:
Since a C++ using declaration isn't allowed in MC++, is there a way to specify that a property, method, event, or field's access can be changed in a derived class, ie. is protected in one class and...
6
by: Matt | last post by:
I'm having difficulty with trying to update a Access memo field through an SQL statement where the value I'm trying to pass is longer than 255 characters. The field is being truncated. I'm using...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
3
by: zlf | last post by:
Hi, I am trying to access private field by code below. But it runs with error. Please tell me what's the problem or any method that may allow me to access private field. Thank you class Love...
4
by: Jack | last post by:
Hi, I have a class with a get-only public bool property. This is good because I want users of this class to ONLY READ it. So I've defined it thus: private bool _IsDirty; public bool IsDirty{...
4
by: Christopher | last post by:
I am surprised this hasn't come up for me more in the past, but the situation is: I need to have an interface that is usable for all I need to have an interface that is only usable for some I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...

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.