PC Review


Reply
Thread Tools Rate Thread

Designing a Collection class

 
 
bg_ie@yahoo.com
Guest
Posts: n/a
 
      25th Jun 2008
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
 
Reply With Quote
 
 
 
 
parez
Guest
Posts: n/a
 
      25th Jun 2008
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);


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      25th Jun 2008
Just a List<MyFile> or a Collection<MyFile> should 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<MyFile> files = 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best method for designing this class Navaneeth.K.N Microsoft C# .NET 3 10th Jun 2008 04:28 PM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
Some help designing a class for a C# beginner! Mark Microsoft C# .NET 3 19th Oct 2004 09:04 AM
Class X Creates Collection of Instances of Class Y. Y's need info from X Jim Frazer Microsoft C# .NET 3 16th Jun 2004 09:17 AM
RaiseEvent from a class contained in a 2nd class collection? Andrew Microsoft Excel Programming 2 6th Jan 2004 04:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:21 AM.