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?
CodePudding user response:
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.
CodePudding user response:
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 ...:
}
CodePudding user response:
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
}
}
CodePudding user response:
You need to tell the compiler that Task is actually a FixedTask.
....
case FixedTask:
switch (((FixedTask)Task).Frequency)
{
case "Daily":
....
This will cast the Task to a FixedTask, there are several other wayt to accomplish the same - take a look at https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions that provides some further details