There are two alternatives.
- 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. - Check the object against the classes of Students and save to different directories accordingly as shown below.
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());
students.ForEach(s => Console.WriteLine (s.GetDirectoryToSave()));
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";
}
}