-1

Is it possible to convert a List<Document> to a class of Documents : List<Document>

For instance, I have a class that looks something like this:

public class Documents : List<Document>
{

}

But I need to convert my List<Document> to Documents.

(Lack of information on this leads me to believe it is either not possible, or I am gravely mis-using these objects.

Deetz
  • 329
  • 1
  • 16
  • You can't. You will need to construct a new `Document` object. A simple casting will not work. – Niyoko Nov 12 '16 at 04:56
  • It's not recommended to subclass `List`. Perhaps you should implement `IEnumerable` and pass in the list to `Documents`? If `Documents` does not have any implementation, however, it's better to simply use `List` rather than `Documents` – Rob Nov 12 '16 at 04:57
  • @Rob By pass in, you mean.... ? – Deetz Nov 12 '16 at 04:58
  • @beardedmogul Provide `Documents` with a list of documents in the constructor, or as a property. – Rob Nov 12 '16 at 04:59
  • 1
    why do you want another class? is there any extra functionality or property you want to add? – M.kazem Akhgary Nov 12 '16 at 05:22
  • There's no clarity regarding the use case, why do you need it, provide ore details – Mrinal Kamboj Nov 12 '16 at 05:35

2 Answers2

1

Method #1

There is currently no default builtin way to cast List<Object> to Objects : List<object>, but instead you could kind of clone the list:

public static Documents CastToDocuments(this List<Document> docs)
{
    var toRet = new Documents();
    toRet.AddRange(docs);
    return toRet;
}

I do also highly suggest reading another question on SO that already asked Why not inherit from List<T>?

Method #2

I just got the idea, that you could add a private list inside your Documents class and implement basic list logic and a ToList() method:

/// <summary>
/// Represents a list of documents.
/// </summary>
public class Documents
{
    /// <summary>
    /// Initialises the private list of documents.
    /// </summary>
    public Documents()
    {
        _docs = new List<Document>();
    }

    /// <summary>
    /// Add a speified document.
    /// </summary>
    /// <param name="doc">This document will be added to the saved documents.</param>
    public void Add(Document doc)
    {
        _docs.Add(doc);
    }

    /// <summary>
    /// Remove a specific document.
    /// </summary>
    /// <param name="doc">This document will be removed from the saved documents.</param>
    public void Remove(Document doc)
    {
        _docs.Remove(doc);
    }

    /// <summary>
    /// Removes all saved documents.
    /// </summary>
    public void Clear()
    {
        _docs.Clear();
    }

    /// <summary>
    /// "Casts" this instance to a list of documents.
    /// </summary>
    /// <returns>Returns all documents inside a list.</returns>
    public List<Document> ToList() => _docs;

    /// <summary>
    /// A list of documents.
    /// </summary>
    private readonly List<Document> _docs;
}

This seems a bit nicer to use because of the following benefits:

  • A nice wrapper for Document
  • More security on the actual list
  • More control of the list
  • The ability to only implement methods, that you really need
  • (And if you need the features of a list, than you could still just return your secure list!)
Community
  • 1
  • 1
TheRealVira
  • 1,444
  • 4
  • 16
  • 28
  • Good read, but our situations are different. _Documents_, in this situation, is simply a list of _Document_. I was just looking for a cleaner way to have plural _Document_. – Deetz Nov 12 '16 at 12:53
  • @beardedmogul added a _cleaner_ method! – TheRealVira Nov 12 '16 at 15:12
  • I, by chance, revisited this answer, and can I just say, Method 2 is genius :) I didn't totally understand what the outcome would be, and although I still don't entirely understand what it's doing, I do enough to implement it. However, I have a problem. I am having trouble implementing a 'Where' on it. Would you be so kind as to help me? – Deetz Dec 04 '16 at 19:15
  • @beardedmogul Great to hear! :) I will work out a solution later on, but meanwhile you could simple use the Where method on ToList(). Simple, but effective ;) – TheRealVira Dec 05 '16 at 14:32
  • I did actually get a working solution. My problem was that I had it executing on an already initialized object, so if I ran the 'where' once, it worked as I would expect, but running it again evaluated only on the previous 'where's results. I changed it to copy the current object and return a 'where' on the copied data, leaving the original object untouched. – Deetz Dec 05 '16 at 14:41
  • Interesting. Normally Where does not alter the current list, but rather returns the result as a copy (list) of the found items! How exactly does your solution look like? – TheRealVira Dec 05 '16 at 14:52
  • If I remember correctly, it looked like this: `public DataObjects Where(System.Func p)` `{` `return dataObjects.Where(p);` `}` (I forget what a predicate exactly looks like :-/ I'm at work. I can post my code when I get home. – Deetz Dec 05 '16 at 15:49
0

You can't convert object of base class to derived class in general and List<T> to MyType : List<T> in particular.

The only option without changing design is to construct new instance of Documents and copy add items from original list to that new instance (possibly there is constructor that takes list as argument).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179