views:

449

answers:

4

Greetings,

I need to include a property in my class which is a collection of System.IO.FileInfo objects. I am not really sure how to do this and how I would add and removed objects from an instance of the the class (I would assume like any other collection).

Please let me know if I need to add more information.

Thank you

Update: Am I approaching this the wrong way? I have read comments that adding to a collection which is a property is bad practice. If this is true what is good practice? I have a bunch of objects I need to store in a collection. The collection will be added to and removed from before a final action will be taken on it. Is this a correct approach or am I missing something?

+1  A: 

File is a static class. So let's assume you meant FileInfo.

There are lots of ways, you can:

  • Expose a private field
  • Use Iterators
  • Expose a private field through a ReadOnlyCollection<>

For example,

class Foo {
    public IEnumerable<FileInfo> LotsOfFile {
        get {
            for (int i=0; i < 100; i++) {
                yield return new FileInfo("C:\\" + i + ".txt");
            }
        }
    }
    private List<FileInfo> files = new List<FileInfo>();
    public List<FileInfo> MoreFiles {
        get {
            return files;
        }
    }
    public ReadOnlyCollection<FileInfo> MoreFilesReadOnly {
        get {
            return files.AsReadOnly();
        }
    }

}

With this code, you can easily add to the property MoreFiles:

Foo f = new Foo();
f.MoreFiles.Add(new FileInfo("foo.txt"));
f.MoreFiles.Add(new FileInfo("BAR.txt"));
f.MoreFiles.Add(new FileInfo("baz.txt"));
Console.WriteLine(f.MoreFiles.Count);
Frank Krueger
Thank you, but you missed one part of my question, I need to ADD to the collection, not possible when it's read only.
Brettski
A: 

One simple way to do this is to create a property as such (sorry for the VB.Net)

Public ReadOnly Property Files As Generic.List(Of IO.File)
    GET
        Return _Files
    END GET
END Property

Where _Files is a private class variable of type Generic.List(Of IO.File), which holds the list of files. That will allow files to be added and removed by calling the functions of the List data type. Some people will probably say this is bad practice, and that you should never expose the collection itself, and instead recode all the necessary functions as separate parameters, which would basically just call the appropriate functions from your private collection.

Kibbee
This is essentially the way to do it: expose the collection as a readonly property. However MS Guidelines say it should be exposed as IList<T> rather than List<T>.
Joe
+1  A: 
using System.Collections.ObjectModel;

public class Foo
 { private Collection<FileInfo> files = new Collection<FileInfo>();
   public Collection<FileInfo> Files { get { return files;} }
 }

//...
Foo f = new Foo();
f.Files.Add(file);
Mark Cidade
A: 

I just make it either a list or dictionary. I'll show both.

class Example
{
    public List<FileInfo> FileList { get; set; }
    public Dictionary<string, FileInfo> Files { get; set; }

    public Example()
    {
        FileList = new List<FileInfo>();
        Files = new Dictionary<string, FileInfo>();
    }

}

You would now use the property as if it were the actual List or Dictionary object.

var obj = new Example();
obj.FileList.Add(new FileInfo("file.txt")); // List<>
obj.Files.Add("file.txt", new FileInfo("file.txt")); // Dictionary<>
// also
obj.Files["file2.txt"] = new FileInfo("file2.txt"); // Dictionary<>

// fetch 
var myListedFile = obj.FileList[0]; // List<>
var myFile = obj.Files["file.txt"]; // Dictionary<>

I prefer the dictionary approach.

Note that since the property is public set, you could replace the entire list or dictionary as well.

obj.Files = new Dictionary<string, FileInfo>();
// or
var otherFiles = new Dictionary<string, FileInfo>();
otherFiles["otherfile.txt"] = new FileInfo("otherfile.txt");
obj.Files = otherFiles;

If you made the property private set, then you could still call Add(), but not reassign the list or dictionary itself.

hurst
It's considered bad practice for properties that are collections to be settable.
Joe
Joe, So what is the "Proper" way to add to a collection which is a property of your class. I have all this items and I need to add or remove from them... What would you suggest?
Brettski