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

Restricting access of class variable to another single class?

Hi all,

The next few paragraphs put my question in context, but feel free to
skip down to the end if you don't care *why* I need the answer! :)

****************************************
*** Start of long-winded description ***
****************************************

I've got some classes that define a tree structure. When the Parent
property of an item is changed, it automatically updates the Children list.

The opposite is also true - if you add an item to a Children list, it's
parent is automatically updated.

The way this second part works, is with an event (fired when items are
added to the list) like this:

children.Added += delegate(object sender,
ChildList<T>.ChildListEventArgs<T> e) { e.Item.parent = (T)this; };

Notice the lowercase p in parent. If I change this to a P, then I'll get
a StackOverflow, because setting the parent, adds to the child list, and
adding to the child list sets the parent. This works fine, because
everything is in one class (the tree is a tree of one type).

Now, I've extended it slightly to support ClassA having children of
ClassB, and ClassB has a parent of ClassA.

My problem, is that from within ClassB, when the child list is modified,
I need to set the parent of ClassA. My current method requires that the
parentClassB property within ClassA be internal (if I use the public
property, I'll overflow the stack again). However, it's important people
(me) don't "accidentally" use this variable when meaning to use the
public property (else my magic code won't get called, undermining the
whole point of me abstracting the tree stuff!).

****************************************
*** End of long-winded description ***
****************************************

So... In short - is there any way to restrict access, like "internal",
but only to subset of classes within the DLL?

If not, the only way to make sure my code is used properly, is to move
ClassA and ClassB into a separate DLL, so only they can set each others
internal data.

The problem with that, is that ClassA and ClassB then need to be public,
and that means anyone can reference my DLL and use my classes, whereas
if everything is in one, they can be internal classes.
Are there any solutions? :-(
Nov 30 '05 #1
4 1340
> So... In short - is there any way to restrict access, like "internal",
but only to subset of classes within the DLL?


What you're after is something like the "friend" designation available
in some other languages. C# doesn't have it, so no.

That said, one way to at least reduce confusion is to expose the
internal "set parent without fiddling with parent's child list"
operation as a method, rather than a property (or, even worse, exposing
the field directly). For example, you could have:

public ClassB Parent
{
get { return this._parent; }
set
{
SetParent(value);
... update parent's children ...
}
}

internal void SetParent(ClassB newParent)
{
this._parent = newParent;
}

Then at least the code within your DLL would know that if it's setting
Parent it's getting the public version, while to do an internal set
(with no side effects) it has to call the method, and the difference
will be more visible (rather than being nothing more than a case
difference: parent versus Parent).

Nov 30 '05 #2
Bruce Wood wrote:
That said, one way to at least reduce confusion is to expose the
internal "set parent without fiddling with parent's child list"
operation as a method, rather than a property (or, even worse, exposing
the field directly). For example, you could have:

public ClassB Parent
{
get { return this._parent; }
set
{
SetParent(value);
... update parent's children ...
}
}

internal void SetParent(ClassB newParent)
{
this._parent = newParent;
}

Then at least the code within your DLL would know that if it's setting
Parent it's getting the public version, while to do an internal set
(with no side effects) it has to call the method, and the difference
will be more visible (rather than being nothing more than a case
difference: parent versus Parent).


I thought about that too (it stops intellisense picking the wrong one
parent/Parent), though it's not as nice as I'd hoped. I'll go with it
for now (since it's the best solution), but I think my whole tree thing
is a bit messy now, so might re-plan it a bit better at some point!
Nov 30 '05 #3
Incidentally, that's why I started using the _xxx notation for private
member fields. I hate stupid prefixes, but boy, does it make it easier
to use Intellisense. No more picking the field instead of the property,
or vice versa.

It also has the advantage that you see the fields first in the
debugger's Local window, so you avoid the endless slow scrolling as the
debugger evaluates all of the properties from A to O just to get to the
"parent" field.

Dec 1 '05 #4
Bruce Wood wrote:
Incidentally, that's why I started using the _xxx notation for private
member fields. I hate stupid prefixes, but boy, does it make it easier
to use Intellisense. No more picking the field instead of the property,
or vice versa.

It also has the advantage that you see the fields first in the
debugger's Local window, so you avoid the endless slow scrolling as the
debugger evaluates all of the properties from A to O just to get to the
"parent" field.


heh, I've used _xxx a few times, but it's so ugly, I always go back to
lower-case for fields and ProperCase(!) for properties. An addition to
your reasons, my reason was so that in a method with its own local
variables, it's easy to distinguish what's "object-scoped", and won't
disappear when this method ends! :)
Dec 1 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
by: Dennis C. Drumm | last post by:
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...
6
by: Danny Tuppeny | last post by:
Hi all, The next few paragraphs put my question in context, but feel free to skip down to the end if you don't care *why* I need the answer! :) **************************************** ***...
1
by: ward | last post by:
Good morning everyone. I'm building a very simple content management site that tracks "tasks." The options available are: 1. Add Task 2. Edit Task 3. View Task 4. Print Task
9
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
2
by: sant.tarun | last post by:
Hi, I am facing some some problem in restricting the access of a variable.... My question is described below..... Let I have two different C source files 'a.c' and 'b.c'. In the file 'a.c'...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.