Collection of object that implement the same interface

  • Thread starter Guthrie Phalanx
  • Start date
G

Guthrie Phalanx

HI all

Bit of a newbie on interfaces, I hope this is not a silly question.

I am stuck on a problem where I want to create a custom collection of
objects, all of whom implement the same interface.
The following code probably explains my situation better than words do :
interface IEvent {
string Title {get; set;}
string Description {get; set;}
string URL {get;}
void Delete();
void Update(int updateID);
}
public class EventType1 :IEvent {
... implementation code for EventType1
}
public class EventType2 : IEvent {
... impleentation code for EventType2
}
public class EventCollection : System.Collections.CollectionBase {
public void Add(IEvent theEvent) {
List.Add(theEvent); //<------------------- this line causes the
error
}
}
On the line indicated, I get the follopwing error :
Event.cs(49): Inconsistent accessibility: parameter type
'Chamber.Components.IEvent' is less accessible than method
'Chamber.Components.EventCollection.Add(Chamber.Components.IEvent)'

What I am trying to do is create a custom collection class that holds any
object that implements IEvent. So, EventType 1 and EventType2 types could be
added to this collection.

Where have I gone wrong? Do I need to have an EventBase class that
implements IEvent, and then make EventType1 and EventType2 inherit from
that? If so, what is the point of having an interface defined in the first
place, when I can define everything I want in the EventBase class?

TIA for any help

- Guthrie
 
J

Jon Skeet [C# MVP]

On the line indicated, I get the follopwing error :
Event.cs(49): Inconsistent accessibility: parameter type
'Chamber.Components.IEvent' is less accessible than method
'Chamber.Components.EventCollection.Add(Chamber.Components.IEvent)'

What I am trying to do is create a custom collection class that holds any
object that implements IEvent. So, EventType 1 and EventType2 types could be
added to this collection.

Where have I gone wrong? Do I need to have an EventBase class that
implements IEvent, and then make EventType1 and EventType2 inherit from
that? If so, what is the point of having an interface defined in the first
place, when I can define everything I want in the EventBase class?

No - all you need to do is make the interface public.
 
G

Guthrie Phalanx

Doh!

I knew it would be something simple. Thanks Jon.

One other question - is this approach valid? Is it the correct of
implementing what I am trying to do?
 
J

Jon Skeet [C# MVP]

Guthrie Phalanx said:
I knew it would be something simple. Thanks Jon.

One other question - is this approach valid? Is it the correct of
implementing what I am trying to do?

Looks fine to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top