Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

class that inherits a collection

0 views
Skip to first unread message

Laoballer

unread,
Dec 18, 2009, 6:38:29 PM12/18/09
to
If I want to create a class that inherits Collection in C# 2.0 do I
need to implement all the methods in Collections?

Family Tree Mike

unread,
Dec 18, 2009, 6:47:20 PM12/18/09
to
On 12/18/2009 6:38 PM, Laoballer wrote:
> 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());
}
}


--
Mike

Peter Duniho

unread,
Dec 18, 2009, 6:57:09 PM12/18/09
to
Laoballer wrote:
> 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. :)

Laoballer

unread,
Dec 18, 2009, 7:20:59 PM12/18/09
to
On Dec 18, 3:57 pm, Peter Duniho <no.peted.s...@no.nwlink.spam.com>
wrote:

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

Michael Ober

unread,
Dec 18, 2009, 7:51:11 PM12/18/09
to
Put the statement

using System.Collections.Generic;

or

using System.Collections.ObjectModel;

at the start of your file. I don't remember which one off the top of my
head.

Mike Ober.

"Laoballer" <laob...@gmail.com> wrote in message
news:6b465fa3-eecc-43dd...@z35g2000prh.googlegroups.com...

Peter Duniho

unread,
Dec 18, 2009, 7:56:37 PM12/18/09
to
Laoballer wrote:
> 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

0 new messages