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
PS 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 eg 12:00) almost everything else is fine i think
PS2 Thank you guys for your help so far!
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)
.
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!");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.