I have a simple process:
event response method
"do something based on specific response" method
validate do something
I'm trying to pass the sender object created by the event through "do something" to Validate:
private void TypeComboBox_Changed(object sender, EventArgs e)
{
int mstrTag = Convert.ToInt32((sender as ComboBox).Tag);
string PrizeTypeSent = (string)(sender as ComboBox).SelectedItem;
choseType(Controls, sender, mstrTag, PrizeTypeSent);
}
private void choseType(Control.ControlCollection listControls,
object sender, int mstrTag, string PrizeTypeSent)
{
foreach (Control c in listControls)
{
// set working variables
if (PrizeTypeSent == "Amount of Prize")
{
MonetaryValidation(Controls, sender, mstrTag, PrizeTypeSent);
// if validated continue
}
}
}
private void MonetaryValidation(Control.ControlCollection listControls,
object sender, int mstrTag, string PrizeTypeSent)
{
string amtTB = "AmountTextBox0";
string prcntTB = "PercentTextBox0";
foreach (Control c in listControls)
{
if (possibility 1)
{
int.TryParse(c.Text, out int percentValue);
if (percentValue > 0)
{
// MessageBox criteria
if (result == DialogResult.OK)
{ goto UseAmount; }
else
{ goto UsePercent; }
}
}
else if (possibility 2) { }
UseAmount:
(sender as MaskedTextBox).Text = "Amount of Prize";
UsePercent:
(sender as MaskedTextBox).Text = "Percent of Prize Budget";
}
}
The preceeding code throws the following run-time exception:
System.NullReferenceException=Object reference not set to an instance of an object.
(...as System.Windows.Forms.MaskedTextBox) returned null.
My expectation was that the complete "sender" object would be passed but obviously not. I've tried "forcing instantiating" with:
object passSender = sender;
both in the initial event method and in the Validation method but get the same run-time exception.
I also tried:
object passSender = new object();
passSender = sender;
but again get the same run-time exception.
What exactly is being passed and why isn't declaring passSender and setting equal to "sender" after creating a new object populating passSender with the initial sender content?
[Note: I coded in COBOL and assembler back in the 1980's and I obviously haven't got my brain sufficiently wrapped around OOP yet.]
Thanx in advance for your time, expertise and patience,
Art