I am trying to access a return value in a getter and use it in a conditional statement to disable a feature. I will organize the code below so you get a better understanding of what is going on since the value is being passed to a interface. I have declared the function inside fixture controller because there is a global variable that the function uses so I then pass the value into a set method into the Interface and both classes extend the interface and use each others methods. I have implemented the set method in the fixtureReport class but I am having issues accessing the value I assign to the function call.
I need the return value to use it in a another function within the fixture report class.
public Interface FixtureView{
// adding function to interface so fixture report can access
void setReceiverType(Boolean receiver);
}
public class fixtureController : FixtureView {
// global variable which the get receivertype method needs
scConfig config;
public bool getReceiverType(){
int receiver = getValue();
if(receiver == nanoReceiver)
{
return false;
}
else
{
return true;
}
}
public partial class fixtureReport : FixtureView{
public void setReceiverType(bool receiver){
fixtureController receiverType = new fixtureController();
// I am storing the function call getReceiverType in a variable here so I can
// use it in this file in a different function but I am unable to access. It does
// not show up in the autofill menu when I start type the variable name
receiver = receiverType.getReceiverType();
}
Ive tried to set the function equal to a variable so it would be easier to manipulate but that didnt work and I tried to just make the function call in the set method but didnt quite understand that.
>Solution :
Are looking for events? Let’s design the interface as
public interface IFixtureView {
// The property itself
// I have declared it of type "bool" but you may actually want "int"
//TODO: Find a better name ReceiverType which is "true" is misleading
public bool ReceiverType { get; set; }
// Event when ReceiverType is changed
public event EventHandler ReceiverTypeChanged;
}
The implementation can be as follow:
public class FixtureController : IFixtureView {
// Backing field with default value
private bool m_ReceiverType = false;
// ReceiverType property which is based on the backing field
public bool ReceiverType {
get => m_ReceiverType;
set {
// are we really changing m_ReceiverType?
if (value == m_ReceiverType)
return;
// If yes, assign
m_ReceiverType = value;
// and let all subscribers know that state is changed
ReceiverTypeChanged?.Invoke(this, EventArgs.Empty);
}
}
public event EventHandler ReceiverTypeChanged;
}
Usage:
FixtureController controller = new FixtureController();
controller.ReceiverTypeChanged += (s, e) => {
FixtureController ctrl = s as FixtureController;
Console.WriteLine($"State is changed, now it's {ctrl.ReceiverType}");
}
...
// When we change the state
// ReceiverTypeChanged will be fired
// And we'll see "State is changed, now it's ..."
controller.ReceiverType = !ReceiverType;