473,568 Members | 3,014 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(Folde r f)
{
_subFolders.Add (f);
f.Parent = this;
}
}

Feb 8 '06 #1
1 1627
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(Folde r 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
3047
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 code looks like this: public sealed class Reference { private static readonly Reference instance = new Reference(); // Make the default...
4
2212
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 the old code allowed me to do what basically was the following assignment: class SomeClass { private Queue q; SomeClass(Queue q)
5
2913
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 an "AppleBasket" which is a class that contains a collection of apples. So, some code: class Fruit{ }
25
3006
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 aware of that fact that if you have a class B, that inherits from A, then List<Bdoes NOT inherit from List<A>. So I understand why the example below...
0
7605
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...
0
7917
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. ...
0
8118
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...
0
7962
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...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.