473,699 Members | 2,800 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 4634
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
2245
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
4992
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
2411
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
6
1700
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
2703
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
1858
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
5033
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
1699
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
1607
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
8704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8623
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
9187
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
9054
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
8936
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
8894
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...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2361
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.