472,968 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

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 1605
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
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
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.