I defined a public delegate and its static members(returning object type) in a separate class and am trying to invoke it from different winforms. These winforms would then check the type returned by the delegate member and then cast it appropriately. So far, so good. But, Visual Studio is complaining that my delegate type declared in each winform is never assigned to and its value will always be null.
The warning is:
Warning CS0649 Field 'AccountReplenish.transferDelegate' is never assigned to, and will always have its default value null ETTA in AccountReplenish.cs
Also, in AccountReplenish, how do I call GetData() of the delegate class? Here's my code:
namespace ETTA.Classes
{
public class DelegateClass
{
public delegate void TransferDelegate(object data);
public TransferDelegate transferDelegate;
private static object _receivedOutput = new object();
public DelegateClass()
{
this.transferDelegate += new ETTA.Classes.DelegateClass.TransferDelegate(ReceiveOutput);
}
public static void ReceiveOutput(object data)
{
_receivedOutput = data;
}
public static object GetData()
{
return _receivedOutput;
}
}
And each of my winform would invoke it as below:
public partial class AccoutnReplenish : Form
{
private ETTA.Classes.DelegateClass.TransferDelegate transferObject;
private object _receivedOutput = new object();
public AccoutnReplenish()
{
this.transferObject= new Classes.DelegateClass.TransferDelegate (ETTA.Classes.DelegateClass.ReceiveOutput);
}
private void button_click(object sender, EventArgs e)
{
AutoReplenishWindow _progressWindow = new AutoReplenishWindow(transferObject);
DialogResult dr = _progressWindow.ShowDialog(this);
if (dr == System.Windows.Forms.DialogResult.OK )
this._receivedOutput = ETTA.Classes.DelegateClass.GetData();
if (this._receivedOutput != null && (this._receivedOutput.GetType() == typeof(string)))
{
string _s = (string)this._receivedOutput;
MessageBox.Show("Result: " + _s);
}
}
}
public partial class AutoReplenishWindow : Form
{
private ETTA.Classes.DelegateClass.TransferDelegate transferObject;
public AutoReplenishWindow()
{
InitializeComponent();
}
public AutoReplenishWindow(ETTA.Classes.DelegateClass.TransferDelegate del)
{
InitializeComponent();
transferObject = del;
}
private async void AutoReplenishWindow_Shown(object sender, EventArgs e)
{
int arg1 = 12;
string _result = await dbUtils.CallFunction(arg1);
if (_result != null && transferObject!= null)
{
transferObject.Invoke(_result );
this.Close();
}
}
}
Any help is appreciated. NH
Edit: I made a few edits(see above) to the code above and it seems to be working ok.