Sorry if this has been covered elsewhere - if so, I missed it and got caught out. To help other plugin developers this is an alert that plugin private fields are shared across form instances.
I'm guessing this is intentional/intended behaviour, just that from the "outside looking in" as a developer it caught me out as completely unexpected.
So to explain ... with this simple test plugin ...
- Code: Select all
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
if (!(JiwaForm is frmDebtor)) return;
var form = (frmDebtor)JiwaForm;
form.Debtor.ReadEnd += (sender, e) => RunTest(form);
}
private string _private;
private void RunTest(frmDebtor form)
{
_private += "A";
form.Tag += "B";
File.AppendAllText("C:\\temp\\jiwalog.txt", _private + "\n");
File.AppendAllText("C:\\temp\\jiwalog.txt", form.Tag + "\n\n");
}
Open a debtor maintenance form, click "next" twice, open a second debtor maintenance form (tab), click "next" twice, go back to the first debtor maintenance form (tab), click "next" twice.
The result is:
- Code: Select all
A
B
AA
BB
AAA
BBB
AAAA
B
AAAAA
BB
AAAAAA
BBB
AAAAAAA
BBBB
AAAAAAAA
BBBBB
However, the private field is in common across the 2 tabs, so in the test above, each debtor tab continues to alter the value from the other tab.
So I have been in the habit of creating a private form field and passing it around the class ...
- Code: Select all
private frmDebtor _form;
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
if (!(JiwaForm is frmDebtor)) return;
_form = (frmDebtor)JiwaForm;
_form.Debtor.ReadEnd += Debtor_ReadEnd;
}
private void Debtor_ReadEnd(object sender, System.EventArgs e)
{
_form.Tag += "B";
}
The solution is to pass everything that you need to persist to your handlers (as the samples the Jiwa guys provide do!). This is often the "sender", but if not, you can lambda it ...
- Code: Select all
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
if (!(JiwaForm is frmDebtor)) return;
var form = (frmDebtor)JiwaForm;
form.Debtor.ReadEnd += (sender, e) => Debtor_ReadEnd(form);
}
private void Debtor_ReadEnd(frmDebtor form)
{
form.Tag += "B";
}
Again, sorry is this is dealt with elsewhere, but I hope this might help others.
Cheers,
Neil.