Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET interfaces
Hi all. I have little problem.
In first method of my code I've loaded data about students from TXT file to List<Student> collection. There are more types of students (classes PrimaryStudent, SecondaryStudent, HightSchoolStudent, UniversityStudent, ExternStudent etc...)
Now, in other method I want to save every kind of Student to different directory. My problem is in that I have all objects in one collection in that interface. How can now I differentiate or what should I do to differentiate all types of students? Please help.
Posted 5-May-12 8:02am
Edited 5-May-12 12:09pm
v2
Comments
VJ Reddy at 6-May-12 3:19am
   
Thank you for accepting the solution.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are two alternatives.
  1. If a method or property can be introduced in the classes, then declare an abstract or virtual method or property in the Student class and override it in the derived classes to return an appropriate directory to save that class, which can be used for saving.
  2. Check the object against the classes of Students and save to different directories accordingly as shown below.
  3. Here to demonstrate the idea I have used Console.WriteLine. The string can be used for saving to a directory with that name using appropriate code.
    void Main()
    {
        List<Student> students = new List<Student>();
        students.Add(new PrimaryStudent());
        students.Add(new SecondaryStudent());
     
        //Alternative 1 If a method or property can be introduced
        //in the Derived classes
        students.ForEach(s => Console.WriteLine (s.GetDirectoryToSave()));
     
        //Alternative 2
        students.ForEach(s => {
            if (s is PrimaryStudent)
                Console.WriteLine (@"C:\PrimaryStudent");
            else if (s is SecondaryStudent)
                Console.WriteLine(@"C:\SecondaryStudent");
     
        });
    }
     
    public abstract class Student {
        public abstract string GetDirectoryToSave();
     
    }
     
    public class PrimaryStudent : Student {
        public override string GetDirectoryToSave(){
            return @"C:\PrimaryStudent";
        }
    }
    public class SecondaryStudent : Student {
        public override string GetDirectoryToSave(){
            return @"C:\SecondaryStudent";
        }
    }
  Permalink  
v2
Comments
Sandeep Mewara at 5-May-12 14:32pm
   
My 5!
Shahin Khorshidnia at 5-May-12 17:10pm
   
Yes, Me too
VJ Reddy at 5-May-12 20:42pm
   
Thank you, Shahin.
VJ Reddy at 5-May-12 20:40pm
   
Thank you, Sandeep.
Mohammad A Rahman at 5-May-12 20:28pm
   
Good use of Polymorphism! I would use your Alternative option 1 as that make perfect sense.
5.
VJ Reddy at 5-May-12 20:49pm
   
Thank you, Rahman.
I agree with you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 DamithSL 165
4 Tomas Takac 144
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 5 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100