Interface for a List < MyObject > ?

S

sloan

Here is one I've been tinkering around with for a few days.


Given the bottom 2 classes and one interface. (at the very bottom)

Is there a way to represent

EmployeeCollection : List<IEmployee>
as an interface?


as in, I'd like to have:

public class EmployeeCollection : IEmployeeCollection
{}

but is still just a
List<IEmployee>


Does that make sense?


I tried this:
public interface IEmployeeCollection :
System.Collections.Generic.IEnumerable<IEmployee>
{}



but that really didn't do it.



To say it another way, I'd like to be able to use an IEmployeeCollection, in
place of the concrete EmployeeCollection.

as in:



IEmployeeCollection icoll = new EmployeeCollection();

and have methods like

icoll.Add ( new Employee ) ;

or
IEmployee ie = new Employee( //constructor params here //);
icoll.Add ( ie ) ;

......................
Here's my code samples:
....................

[Serializable]

public class EmployeeCollection : List<IEmployee>

{

public EmployeeCollection()

{

System.Console.WriteLine("New EmployeeCollection has been created.");

}

}



public interface IEmployee

{

Guid EmployeeUUID { get; }

string LastName { get; }

string FirstName { get; }

DateTime HireDate { get; }

}







[Serializable]

public class Employee : IEmployee

{

private Guid _employeeUUID ;

private string _lastName = string.Empty;

private string _firstName = string.Empty;

private DateTime _hireDate = DateTime.MinValue; //yes a nullable would be
better here, just demo code

private Employee() {} //unnecessary, but a placeholder

public Employee(System.Guid empUUID , string firstName, string lastName,
DateTime hireDate)

{

this._employeeUUID = empUUID;

this.FirstName = firstName;

this.LastName = lastName;

this.HireDate = hireDate;

}

#region IEmployee Members

public Guid EmployeeUUID

{

get { return _employeeUUID; }

set { this._employeeUUID = value; }

}

public string LastName

{

get { return _lastName; }

set { this._lastName = value; }

}

public string FirstName

{

get { return _firstName; }

set { this._firstName = value; }

}

public DateTime HireDate

{

get { return _hireDate; }

set { this._hireDate = value; }

}

#endregion

}
 
N

Nicholas Paldino [.NET/C# MVP]

You could say:

public interface IEmployeeCollection : IList<IEmployee> {}

And then define EmployeeCollection like this:

public class EmployeeCollection : List<IEmployee>, IEmployeeCollection

That should give you what you are looking for. Since List<T> implements
IList<T>, you shouldn't have to do any implementation work.
 
S

sloan

Nicholas,

THANKS.

Now its clear
IList<IEmployee>

I greatly appreciate it.





Nicholas Paldino said:
You could say:

public interface IEmployeeCollection : IList<IEmployee> {}

And then define EmployeeCollection like this:

public class EmployeeCollection : List<IEmployee>, IEmployeeCollection

That should give you what you are looking for. Since List<T>
implements IList<T>, you shouldn't have to do any implementation work.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

sloan said:
Here is one I've been tinkering around with for a few days.


Given the bottom 2 classes and one interface. (at the very bottom)

Is there a way to represent

EmployeeCollection : List<IEmployee>
as an interface?


as in, I'd like to have:

public class EmployeeCollection : IEmployeeCollection
{}

but is still just a
List<IEmployee>


Does that make sense?


I tried this:
public interface IEmployeeCollection :
System.Collections.Generic.IEnumerable<IEmployee>
{}



but that really didn't do it.



To say it another way, I'd like to be able to use an IEmployeeCollection,
in place of the concrete EmployeeCollection.

as in:



IEmployeeCollection icoll = new EmployeeCollection();

and have methods like

icoll.Add ( new Employee ) ;

or
IEmployee ie = new Employee( //constructor params here //);
icoll.Add ( ie ) ;

.....................
Here's my code samples:
...................

[Serializable]

public class EmployeeCollection : List<IEmployee>

{

public EmployeeCollection()

{

System.Console.WriteLine("New EmployeeCollection has been created.");

}

}



public interface IEmployee

{

Guid EmployeeUUID { get; }

string LastName { get; }

string FirstName { get; }

DateTime HireDate { get; }

}







[Serializable]

public class Employee : IEmployee

{

private Guid _employeeUUID ;

private string _lastName = string.Empty;

private string _firstName = string.Empty;

private DateTime _hireDate = DateTime.MinValue; //yes a nullable would be
better here, just demo code

private Employee() {} //unnecessary, but a placeholder

public Employee(System.Guid empUUID , string firstName, string lastName,
DateTime hireDate)

{

this._employeeUUID = empUUID;

this.FirstName = firstName;

this.LastName = lastName;

this.HireDate = hireDate;

}

#region IEmployee Members

public Guid EmployeeUUID

{

get { return _employeeUUID; }

set { this._employeeUUID = value; }

}

public string LastName

{

get { return _lastName; }

set { this._lastName = value; }

}

public string FirstName

{

get { return _firstName; }

set { this._firstName = value; }

}

public DateTime HireDate

{

get { return _hireDate; }

set { this._hireDate = value; }

}

#endregion

}
 

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