473,503 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Collections and Parent Reference


I have a class, Folder, that I want to hold a reference to its parent
Folder as well as its subfolders.

I'm using List<Folder> to hold its list of sub folders. What I want to
do is when a folder is added to this list, automatically set the parent
reference.

But this seems impossible. It seems like I need to keep List<Folder>
private and expose an "AddFolder" method that adds the folder to the
list, and then also sets the folder's parent reference.
Something like this:

class Folder
{
private List<Folder> _subFolders;

public void AddFolder(Folder f)
{
_subFolders.Add(f);
f.Parent = this;
}
}

Feb 8 '06 #1
1 1623
Hi Brian,
the code you have shown is a good example of encapsulation which is a good
concept to model in your code. You have hidden the underlying datatype from
the user and encapsulated your processing. If you expose your data to the
user directly then they may perform undesirable actions upon it. Ideally
keep your data type encapsulated inside the class and provide these accessor
methods when possible.

Mark.

"Brian P" wrote:

I have a class, Folder, that I want to hold a reference to its parent
Folder as well as its subfolders.

I'm using List<Folder> to hold its list of sub folders. What I want to
do is when a folder is added to this list, automatically set the parent
reference.

But this seems impossible. It seems like I need to keep List<Folder>
private and expose an "AddFolder" method that adds the folder to the
list, and then also sets the folder's parent reference.
Something like this:

class Folder
{
private List<Folder> _subFolders;

public void AddFolder(Folder f)
{
_subFolders.Add(f);
f.Parent = this;
}
}


Feb 8 '06 #2

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

Similar topics

13
3036
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
4
2205
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
5
2909
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
25
2995
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
0
7093
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...
0
7287
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,...
0
7348
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
7467
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...
0
5592
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,...
1
5021
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...
0
4685
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...
0
3175
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...
0
1519
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 ...

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.