0

I am trying to populate the List template with a class (Student) inheriting from two interfaces (IIndividual and IGuardian). Population succeeds quite OK. The problem am having now is that the last element replaces the older elements and overrides them. Am confused.

interface IIndividual
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

interface IGuardian
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

class Student: IIndividual, IGuardian
{
    String IIndividual.Name { get; set; }
    String IIndividual.Number { get; set; }
    String IIndividual.Address { get; set; }
    String IGuardian.Name { get; set; }
    String IGuardian.Number { get; set; }
    String IGuardian.Address { get; set; }

    public void output()//testing
    {
        IIndividual i = this as IIndividual;
        Console.WriteLine("Individual Name = {0}", i.Name);
        Console.WriteLine("Individual Number = {0}", i.Number);
        Console.WriteLine("Individual Address= {0}", i.Address);
        IGuardian g = this as IGuardian;
        Console.WriteLine("Guardian Name = {0}", g.Name);
        Console.WriteLine("Guardian Number = {0}", g.Number);
        Console.WriteLine("Guardian Address= {0}", g.Address);
    }
}


static void Main(string[] args)
{
    List<Student> s = new List<Student>();
    Student ss = new Student();
    IIndividual ii = ss as IIndividual;
    IGuardian gg = ss as IGuardian;
    gg.Name = "Kinyo Yemi";
    gg.Number = "08012345678";
    gg.Address = "Bunker Street";

    ii.Name = "Hope Ajayi";
    ii.Number = "08185543124";
    ii.Address = "Metropolitan Council";
    s.Add(ss);
    /////////////////////
    gg.Name = "Awwal Kadi";
    gg.Number = "08072523245";
    gg.Address = "R32 Damboa";

    ii.Name = "Morak Suleiman";
    ii.Number = "08535755543";
    ii.Address = "Sederal Low Cost";
    s.Add(ss);
    foreach (var x in s)
    {
        x.output();
        Console.WriteLine();
    }
    Console.Read();
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
Kinyo356
  • 135
  • 13
  • 6
    That's not what's happening. `ss`, `ii`, and `gg` are all the same object from beginning to end. That's how reference types work. You only ever create one object, `Student ss = new Student()` – 15ee8f99-57ff-4f92-890c-b56153 Sep 25 '17 at 18:31

3 Answers3

4

Because you only ever create one student:

Student ss = new Student();

But you add it to the list twice:

s.Add(ss);
//...
s.Add(ss);

If you want to add a second student, create a new one:

Student ss2 = new Student();

Then populate that object with values and add it to the list:

s.Add(ss2);

(Or even just re-assign the same variable by writing ss = new Student(); again.)

No matter how many type changes or variables you use, if you only ever create one instance of the object then all of your variables are pointing back to that same instance.

David
  • 208,112
  • 36
  • 198
  • 279
1

You're not making a new student - you're adding the same student twice.

The second assignment overwrites the first.

Insert a new 'new' after the 2nd s.add(ss) (marked '///here///')

Then reassign your interfaces...

static void Main(string[] args)
    {
        List<Student> s = new List<Student>();
        Student ss = new Student();
        IIndividual ii = ss as IIndividual;
        IGuardian gg = ss as IGuardian;
        gg.Name = "Kinyo Yemi";
        gg.Number = "08012345678";
        gg.Address = "Bunker Street";

        ii.Name = "Hope Ajayi";
        ii.Number = "08185543124";
        ii.Address = "Metropolitan Council";
        s.Add(ss);
        ////////**here**/////////////
        ss = new Student();
        ii = ss as IIndividual;
        gg = ss as IGuardian;

        /////////////////////
        gg.Name = "Awwal Kadi";
        gg.Number = "08072523245";
        gg.Address = "R32 Damboa";

        ii.Name = "Morak Suleiman";
        ii.Number = "08535755543";
        ii.Address = "Sederal Low Cost";
        s.Add(ss);
        foreach (var x in s)
        {
            x.output();
            Console.WriteLine();
        }
            Console.Read();
    }
FastAl
  • 6,194
  • 2
  • 36
  • 60
1

As others have pointed out, you only ever create one student object so of course you wont see two in your list. However there are bigger issues here.

What you are doing is actually kind of weird. IIndividual and IGuardian actually describe the exact same thing:

interface IIndividual
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

interface IGuardian
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

Then oddly enough your Student class explicitly implements both:

class Student: IIndividual, IGuardian
{
    String IIndividual.Name { get; set; }
    String IIndividual.Number { get; set; }
    String IIndividual.Address { get; set; }
    String IGuardian.Name { get; set; }
    String IGuardian.Number { get; set; }
    String IGuardian.Address { get; set; }
}

Does it really make sense to do it that way?

What if you were to change your interfaces and classes to this:

public interface IIndividual
{
    string Name { get; set; }
    string Number { get; set; }
    string Address { get; set; }
}

public interface IGuardian : IIndividual
{
    // Only things specific to Guardians should be here
    // If there is nothing specific to Guardians, 
    // dont have a IGuardian interface at all

    string WorkPhone { get; set; }
}

public class Student : IIndividual
{
    //Guardian should be a property of a Student
    public IGuardian Guardian { get; set; }

    public string Name { get; set; }
    public string Number { get; set; }
    public string Address { get; set; }
}

public class Guardian: IGuardian
{       
    public string Name { get; set; }
    public string Number { get; set; }
    public string Address { get; set; }
    public string WorkPhone { get; set; }
}

The benefit of doing it this way is simple, a student and a guardian are both individuals right? A student is not both an Individual and a Guardian, a Student is simply an individual. You should create a Guardian class that implements IGuardian, then make Student have a property of IGuardian.

Another thing you should consider is your output() method. If you want to just see a string representation of your object, you can override the ToString() method of object.

I made a fiddle here that cleans up your code a bit and demonstrates my answer a bit.

maccettura
  • 10,514
  • 3
  • 28
  • 35