Question

All, I have an application that needs to attach and detach multiple SQL databases regularly. I want to create a class that holds all of these databases as a collection that can be iterated over. To do this I am inheriting from ICollection, but there is some thinkg I am not understanding:

class SqlDataBases : ICollection<SqlDb>
{
    private List<SqlDb> dbColl;

    public SqlDataBases()
    {
        dbColl = new List<SqlDb>();
    }

    // Add an index to the collection.
    public SqlDb this[int _nIndex]
    {
        get { return (SqlDb)dbColl[_nIndex]; }
        set { dbColl[_nIndex] = value; }
    }

    // et al.
}

public class DbEnumerator : IEnumerator<SqlDb>
{
    // ...
}

class SqlDb
{
    private string strMdfFullPath;
    private string strLdfFullPath;
    private bool bIsAttached;

    public SqlDb(string _strMdfFullPath, string _strLdfFullPath, bool _bIsAttached)
    {
        this.strMdfFullPath = _strMdfFullPath;
        this.strLdfFullPath = _strLdfFullPath;
        this.bIsAttached = _bIsAttached;
    }
}

My question is "why inherit from ICollection at all, when you have to add methods such as 'Add', 'Contains' etc. yourself? Or do you have to do this yourself as suggested in MSDN? I have been reading "C# in a Nutshell" and this question is something that stands out unaddressed in this great book.

I apologise, I know I am missing something here...

Was it helpful?

Solution

My question is "why inherit from ICollection at all, when you have to add methods such as 'Add', 'Contains' etc. yourself?

ICollection<T> is an interface - it just specifies the members that you have to implement. If you want to derive from something which already has an implementation, look at Collection<T>. You would implement the interface yourself if you wanted to create your own collection data structure with its own special characteristics - I doubt that you want to do that.

To be honest, it's not clear why you want your own class here at all - why not just use List<SqlDb> in the client code directly?

OTHER TIPS

Have you thought about using List<SqlDb> directly?

It's not obvious to me what you hope to gain by the SqlDataBases class.

If you can't use List<SqlDb> directly, think about inheriting from List<SqlDb> rather than ICollection<SqlDb>.

Interfaces are only skeletons. They aren't containing any logic, they are containing only the signatures of all method, property etc.

Interfaces

The reason to inherit from ICollection<> is to create your own custom collection type. Then, anywhere a method has an input variable of ICollection<>, because you inherited from ICollection<>, your new custom collection type can be passed in.

In your case, you could simply use List<> until you find a need to create a custom collection.

Answering "why inherit from ICollection at all, when you have to add methods such as 'Add'":

you don't need to inherit ICollection if you don't need/want to have Add/Next etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top