Click here to Skip to main content
11,805,266 members (57,113 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET
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 7:02am
Edited 5-May-12 11:09am
VJ Reddy at 6-May-12 3:19am
Thank you for accepting the solution.

1 solution

Rate this: bad
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)
    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";
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.
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 315
1 Richard Deeming 200
2 Sergey Alexandrovich Kryukov 170
3 CPallini 160
4 CHill60 150
0 OriginalGriff 3,255
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 5 May 2012
Copyright © CodeProject, 1999-2015
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