473,770 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4639
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*******@prim acode.com> wrote in message
news:OP******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP12.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*******@prim acode.com> wrote in message
news:OP******** ******@TK2MSFTN GP12.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.PrivateM ethod(); //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*******@prim acode.com> wrote in message
news:%2******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP12.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*******@prim acode.com> wrote in message
news:OP******** ******@TK2MSFTN GP12.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*******@prim acode.com>
| From: "Dennis C. Drumm" <de*******@prim acode.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.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 206-15-139-8.dialup.ziplin k.net 206.15.139.8
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1912 75
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
2249
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 question, let it have access to a few private/protected members. this problem is cropping up several times. giving the friend class full access does not seem right n at the same time the function(s) r not reqd by anyone else.
9
4996
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 pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
7
2420
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
6
1704
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 is made public in a derived class ?
6
2711
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 a simple INSERT INTO sql command. Anyone know a way around this? Sample strSQL = "INSERT INTO tblListings " & _ "(Dir, Street, City, State, " & _ "AFirst, Price, ALast, MainPhoto, Summary, Details,
9
1861
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 to the order */ }
3
5035
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 { private string Name;
4
1702
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{ get{ return _IsDirty; } }
4
1612
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 do not really know of a good way to achieve this. If I use friend functions, I can no longer make methods virtual, right? Example:
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.