Using .NET 5 and C# 9.
I am trying to reduce code written to eliminate mistakes down the road. This question may be related to this one.
I have a Interface defined and I want to have the boolean values default to false:
public interface IMenuActionInterface
{
public bool CanConnect => false;
public bool CanDisconnect => false;
public bool CanHome => false;
public bool CanPoll => false;
public bool CanRunAutomatically => false;
public bool CanRunManually => false;
public bool CanPurge => false;
}
Then in each Interface implementation I want to just define the properties that are true:
internal class NotConnected : IMenuActionInterface
{
public bool CanConnect => true;
}
I have a variable defined as IMenuActionInterface and assign that variable various implementations of IMenuActionInterface:
private IMenuActionInterface _robotState;
_robotState = robot.SetOptions(Trigger.Connect)
//Code for SetOptions:
public IMenuActionInterface SetOptions(Trigger trigger)
{
switch (trigger)
{
case Trigger.Connect:
return new Connected();
case Trigger.Disconnect:
return new NotConnected();
case Trigger.Home:
return new Homed();
case Trigger.StartRunning:
return new Running();
case Trigger.StopRunning:
return new Homed();
case Trigger.StartPurging:
return new Purging();
case Trigger.StopPurging:
return new Homed();
default:
throw new ArgumentOutOfRangeException(nameof(trigger), trigger, null);
}
}
In the case where I return a new NotConnected() instance of _robotState.CanConnect returns false. The debugger seems to display both the default value AND the overridden class value.
What is happening here?
I want to define all default properties in my interface and then just define properties in the implementation of the interface that need to have a different value - how?
EDIT The code actually returns the default false.
Edit 2
NotConnected class looks like this:
internal class NotConnected : IMenuActionInterface
{
public bool CanConnect => true;
}
Homed class looks like this:
internal class Homed : IMenuActionInterface
{
internal bool CanDisconnect => true;
internal bool CanHome => true;
internal bool CanPoll => true;
}
If I assign 'test = new Homed()' the value of test.CanDisconnect should return true. It does not. The output of my simplified code:
IMenuActionInterface test = new NotConnected();
Debug.WriteLine(test.CanDisconnect);
test = new Homed();
Debug.WriteLine(test.CanDisconnect);
is:
False
False
Adding even more data to this I edited a dotnetfiddle to reflect my issue and it worked as expected (False, True). I also created a .NET6 console app in visual studio and it works fine as well (False, True). I then created a winforms app with .net6 and this pattern does not work. I get:
False
False
rather then the expected:
False
True
There is definitely different behaviour between .NET6 Console app and .NET6 winform app.
I found the issue....
I looked very carefully at my code and found that the keyword has to be public on the properties in both the Interface and the class that implements the interface.
If both are set to public then all is good:
In fact it seems the compiler is very particular with how the property is defined. These both work:
interface IMenuActionInterface
{
bool CanConnect => false;
bool CanDisconnect => false;
}
public class Testing : IMenuActionInterface
{
}
public class DoubleTesting : IMenuActionInterface
{
public bool CanDisconnect => true;
}
-and-
interface IMenuActionInterface
{
bool CanConnect => false;
public bool CanDisconnect => false;
}
public class Testing : IMenuActionInterface
{
}
public class DoubleTesting : IMenuActionInterface
{
public bool CanDisconnect => true;
}
It seems that if I use any other combination of modifier then it does not like it.