Hi,

I'm doing a short test for a job interview that I have. The code is C# and I am very new to this (2 days :S).

One of the questions is. Write method to get all IPeople - Write a method in the SchoolManager to get all IPeople (Teachers and Students) in the system. Your method should return an IEnumerable of IPeople. You can make use of the exisiting GetAllTeachers and GetAllStudents methods in the SchoolManager or the Teacers and Students properties on the Course.

In a previous question I created the interface (hopefully correctly)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace School.Code
{
    public interface IPeople
    {
        string FirstName
        {
            get;
            set;
        }
        string LastName
        {
            get;
            set;
        }
        Int64 DepartmentId
        {
            get;
            set;
        }
        DateTime DateOfBirth
        {
            get;
            set;
        }
    }
}

And in my PartialClasses.cs I've added : IPeople to both the Teacher and Student Class.

I am now completely stuck as to how I create a GetAllIPeople ENumberable method.

Any help would be greatly appreciated.

Recommended Answers

All 3 Replies

IEnumerable<IPeople> GetAllPeople() {
    foreach (IPeople person in GetAllTeachers()) {
        yield return person;
    }
    foreach (IPeople person in GetAllStudents()) {
        yield return person;
    }
}

You'll have to fix the errors because I don't know what the classes are that contain GetAllXXX() methods.

commented: Post was exactly what I was looking for. +0

Thank you momerath for the answer.

That seems to be working how ever when I try and bind the result to a GridView I get this error

Property accessor 'FullName' on object 'School.Code.Student' threw the following exception:'Object does not match target type.'

Now fullname refers to the partial class in Student and Teacher like so.

public partial class Teacher : IPeople
    {
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", FirstName, LastName);
            }
        }
    }

    public partial class Student : IPeople
    {
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", FirstName, LastName);
            }
        }
    }

What is it I am doing wrong here? Sorry to be an absolute ameteur with this.

Never Mind.

After looking into a further question I only need to bind this to a ListView and bind the data that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.