class that inherits a collection

  • Thread starter Thread starter Laoballer
  • Start date Start date
L

Laoballer

If I want to create a class that inherits Collection in C# 2.0 do I
need to implement all the methods in Collections?
 
If I want to create a class that inherits Collection in C# 2.0 do I
need to implement all the methods in Collections?

Not necessarily. You can do this:

class Car
{
}
class Traffic : List<Car>
{
}

class Program
{
static void Main(string[] args)
{
Traffic t = new Traffic();
t.Add(new Car());
}
}
 
Laoballer said:
If I want to create a class that inherits Collection in C# 2.0 do I
need to implement all the methods in Collections?

That all depends on what "Collection" class you are talking about. When
inheriting a class, the only methods you _have_ to implement are the
abstract methods.

Ask a less vague question, and you'll get a less vague answer. :)
 
That all depends on what "Collection" class you are talking about.  When
inheriting a class, the only methods you _have_ to implement are the
abstract methods.

Ask a less vague question, and you'll get a less vague answer.  :)

So what I'm trying to do is utilize a node class and an nodelist class
that is from an example on msdn located here.
http://msdn.microsoft.com/en-us/library/ms379572(VS.80).aspx

When I compile my code with a single declaration of a node object in
main I get the following error

The type or namespace name 'Collection' could not be found(are you
missing a using directive or an assembly reference?)

I'm using VS2005. So it looks like it's giving me an error because
Collection doesn't exist. If I change collection to ICollection it
gives me 9 errors that states that I haven't implemented
count,IsReadOnly, CopyTo, etc... in
Systems.Collections.Generic.ICollection
 
Laoballer said:
So what I'm trying to do is utilize a node class and an nodelist class
that is from an example on msdn located here.
http://msdn.microsoft.com/en-us/library/ms379572(VS.80).aspx

When I compile my code with a single declaration of a node object in
main I get the following error

The type or namespace name 'Collection' could not be found(are you
missing a using directive or an assembly reference?)

As the compiler error suggests, you may be missing either a using
directive or an assembly reference.

Unfortunately, the article you are following is wrong. It says the
Collection<T> class is in the System.Collections.Generic namespace, but
it's not.

My guess is that at some point in time, before .NET 2.0 was released,
Collection<T> class was actually in the System.Collections.Generics
namespace, but that it got moved before that version of .NET was
actually released and no one ever fixed the article.

Anyway, you should be able to add the correct using directive -- "using
System.Collections.ObjectModel;" -- to your source code file and get the
compiler error to go away.
I'm using VS2005. So it looks like it's giving me an error because
Collection doesn't exist. If I change collection to ICollection it
gives me 9 errors that states that I haven't implemented
count,IsReadOnly, CopyTo, etc... in
Systems.Collections.Generic.ICollection

Sure. ICollection<T> is completely different from Collection<T>. It's
an interface, and as such a class that is declared as implementing that
interface does in fact have to implement everything in the interface.

But ICollection<T> isn't what the article is talking about. It appears
to me it's referring to the System.Collections.ObjectModel.Collection<T>
class, and so what you really need is the appropriate using directive
(or you could use the fully-qualified name, but I presume you want your
code to look as much as possible like the code in the article).

Pete
 
Back
Top