0

i have two interfaces IAppointment and IAppointments : IList<IAppointment> in the second class i have 3 members

 public interface IAppointments : IList<IAppointment>
{
    bool Load();
    bool Save();
    IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date);
}

Of which i can only implement the first 2 in the class Appointments and i get errors with whatever method i tried for the 3rd one and i get the same 14 errors always (about "Appointments does not implement interface member IAppointment.GetEnumerator() , .Count , .Remove , .Contains and some others

also this is the other one

public interface IAppointment
{
    DateTime Start { get; }
    int Length { get; }
    string DisplayableDescription { get; }
    bool OccursOnDate(DateTime date);
}

where here i probably need to implement those in a class too , Sorry about my bad explanation but maybe i havent understood the actual problem

P.S. both the interface/classes are being used in another partial class which runs without errors

Update:

My only problem now is that i don't know how to implement the 1st member of the IAppointment (What should it's return type be?? since its the Starting time of the Appointment e.g. 12:00) almost everything else is fine i think

P.S.2 Thank you guys for your help so far!

Shiroe
  • 65
  • 1
  • 1
  • 10

2 Answers2

1

Because your IAppointments interface derives from IList<T>, your Appointments class must implement all members of IList<T> and any interfaces which that interface derives from. GetEnumerator() comes from IEnumerable<T>, which IList<T> derives from.

Unless you use an approach such as composition where you expose an IList<T> property on IAppointments to get a list on which to perform operations such as indexing etc., you will need to implement all the members of IList<T>, ICollection<T> and IEnumerable<T> in your Appointments class.

I think your best solution is something like this:

public interface IAppointments
{
    IList<IAppointment> TheAppointments { get; }

    bool Load();
    bool Save();
    IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date);
}

Then you would access the TheAppointments property in your Appointments class to provide the base of your implementation of GetAppointmentsOnDate(DateTime).

Martin Costello
  • 9,672
  • 5
  • 60
  • 72
0

As noted in the comments, you cannot only implement a specific set of methods for an interface and as your IAppointments interface derives from IList<IAppointment>, the implementing class must also implement all members of the IList interface in addition to the members of IAppointments.

The following class definition will achieve this:

using System.Collections.ObjectModel;

public class Appointments : Collection<IAppointment>, IAppointments
{
    public bool Load()
    {
        return true;
    }

    public bool Save()
    {
        return true;
    }

    public IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date)
    {
        return new List<IAppointment>();
    }
}

This will give you access to all the methods on IList (since Collection<T> implements IList<T>) and IAppointment allowing you to write code such as the following (assuming your class that implements IAppointment is called Appointment and I have divined the intentions of your code correctly):

var appointments = new Appointments();

if (appointments.Load() == true) // from IAppointments
{
    var totalAppointmentCount = appointments.Count(); // from IList through Collection<T>
    var numberOfAppointmentsToday = appointments.GetAppointmentsOnDate(DateTime.Now.Date).Count(); // from IAppointments

    var newAppointment = new Appointment();

    appointments.Add(newAppointment); // from IList through Collection<T>

    if (appointments.Save() == true) // from IAppointments
    {
        Console.WriteLine("All saved, happy days!");
    }
}
Sean Airey
  • 6,352
  • 1
  • 20
  • 38
  • Inheriting List is rarely a good idea. The OP should most likely choose composition over inheritance, as in martin_costello's answer. – Vincent van der Weele Apr 27 '14 at 18:55
  • @Heuster why is inheriting from `List` a bad idea? I'm curious, I've never done it but I would like to know why it's not the best approach. – Sean Airey Apr 27 '14 at 18:58
  • Ok so I found this SO question (which has a lot of great information on why you shouldn't inherit from `List`): http://stackoverflow.com/questions/21692193/why-not-inherit-from-listt and I have edited my answer to reflect. Since the question was "how to implement this specific interface" I'll leave my answer here but edited to reflect the best-practise guidelines on inheriting from collection classes. – Sean Airey Apr 27 '14 at 19:06
  • @Sean your post Helped a lot and thank you but i will need your help for the Appointment class that you assumed correctly, please check the post because i updated it with some additional code. – Shiroe Apr 27 '14 at 23:51
  • @AngeloB. You have declared a property in an interface, you simply need to implement a property with the same signature, i.e. a type of `DateTime` and a `get` accessor. Since you have only declared a `get` accessor, this property will be read-only. You can get more information here: http://msdn.microsoft.com/en-us/library/64syzecx.aspx if you need help on properties in general, then the relevant documentation is here: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx which includes links to helpful pages such as "Using properties": http://msdn.microsoft.com/en-us/library/w86s7x04.aspx – Sean Airey Apr 28 '14 at 00:39