By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Designing a Collection class

P: n/a
Hi,

I have a hypothetical class called MyFiles which stores an array of
objects of type MyFile. MyFiles contains functions like
GetOldestFile(), GetLargestFile(), etc.

class MyFiles
{
MyFile[] myFiles;

public MyFiles(MyPath path)
{
myFiles = ...
}
}

When implementing the MyFiles class, is there any way to expose the
private members of MyFile for MyFiles? Or must I create accessors? For
me, it feels like MyFiles is inheriting from MyFile even though this
inheritance is not specified in code. Does this make sense?

Thanks for your help,

Barry
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 25, 7:28 am, bg...@yahoo.com wrote:
Hi,

I have a hypothetical class called MyFiles which stores an array of
objects of type MyFile. MyFiles contains functions like
GetOldestFile(), GetLargestFile(), etc.

class MyFiles
{
MyFile[] myFiles;

public MyFiles(MyPath path)
{
myFiles = ...
}

}

When implementing the MyFiles class, is there any way to expose the
private members of MyFile for MyFiles? Or must I create accessors? For
me, it feels like MyFiles is inheriting from MyFile even though this
inheritance is not specified in code. Does this make sense?

Thanks for your help,

Barry
If i have understood you correctly,
you could do the following

class MyFiles : List<MyFile>
{
public new MyFile this[int a]
{
get
{
return base[a];
}
}
}

public class MyFile
{
public string Name { get; set; }

}
in your code, you can have

MyFiles f = new MyFiles();
MyFile ff = new MyFile();
ff.Name = "P";

f.Add(ff);

Console.WriteLine(f[0].Name);
Jun 27 '08 #2

P: n/a
Just a List<MyFileor a Collection<MyFileshould do.

Note that LINQ might also solve a lot of those "biggest/oldest" issues
too... (note you could add extra extension methods to avoid the two-
pass approach here...)

List<MyFilefiles = new List<MyFile>();
long biggestSize = files.Max(file =file.Size);
MyFile biggestFile = files.FirstOrDefault(file =file.Size ==
biggestSize);
DateTime oldestDate = files.Min(file =file.Created);
MyFile oldestFile = files.FirstOrDefault(file =file.Created
== oldestDate);

Marc
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.