Click here to Skip to main content
11,920,211 members (44,097 online)
Rate this:
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 8:02am
Edited 5-May-12 12:09pm
VJ Reddy 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 5-May-12 14:32pm
My 5!
Shahin Khorshidnia 5-May-12 17:10pm
Yes, Me too
VJ Reddy 5-May-12 20:42pm
Thank you, Shahin.
VJ Reddy 5-May-12 20:40pm
Thank you, Sandeep.
Mohammad A Rahman 5-May-12 20:28pm
Good use of Polymorphism! I would use your Alternative option 1 as that make perfect sense.
VJ Reddy 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 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,153
3 George Jonsson 2,810
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.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