I am new to C# and I have a project in which I have a parent class Task with derived classes FixedTask and RelativeTask.
FixedTask and RelativeTask have different properties, named differently. And I need to perform some computations with each Task to compute some workload.
A Project contains different Tasks which can be of the various types e.g. FixedTask and RelativeTask. I was planning on doing something like this:
foreach (Task Task in this.Tasks)
{
switch (Task)
{
case FixedTask:
switch (Task.Frequency)
{
case "Daily":
// do some calculations
// other frequency cases
}
// other Task cases
}
But I receive an error that Task does not have a particular parameter because it is implemented in FixedTask but not in RelativeTask.
I would have though that the switch pattern allowed C# to understand which type he has to consider but apparently not.
What is the proper way of achieving what I want?
Pattern matching: case FixedTask f.
But you shouldn't. Your base class should not know nor care how the derived classes are implemented.
The purpose of OO and inheritance is that derived classes contain their own logic. So if your task runner wants to execute a task, do so:
foreach (Task task in this.Tasks)
{
task.Execute();
}
And then each Task-deriving class has the logic in their overridden Execute() method.
Pattern matching helps:
foreach (Task Task in this.Tasks)
{
switch (Task)
{
case FixedTask ft:
switch (ft.Frequency)
{
case "Daily":
...
}
case RelativeTask rt:
switch (rt.OtherProperty)
{
case ...:
}
As a quick (but not the best) amendment, I suggest pattern matching, i.e.
foreach (Task task in Tasks) {
if (task is FixedTask fixedTask) {
// From now on you work with fixedTask
switch (fixedTask.Frequency) {
...
}
}
else if (task is RelativeTask relativeTask) {
// From now on you work with relativeTask
}
}