What is passed when working with generated system "sender" object?

Art Hansen 566 Reputation points
2021-02-03T13:23:11.64+00:00

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

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,907 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,097 questions
0 comments No comments
{count} votes

Accepted answer
  1. Duane Arnold 3,216 Reputation points
    2021-02-03T18:06:58.107+00:00

    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?

    The sender in this case will be the control the event method is linked to.

    If the control, an object, is a textbox control, then sender will be the textbox type.

    In order to address the textbox object that was passed into the event, you have to cast sender to be a textbox.

    Textbox passedTBX = (Textbox)sender;

    string name = passedTBX.name;
    string text = passedTBX.text;


1 additional answer

Sort by: Most helpful
  1. Viorel 118.4K Reputation points
    2021-02-03T14:23:27.367+00:00

    If sender is not a MaskedTextBox (for example, it is ComboBox), then ‘sender as MaskedTextBox’ will be null.

    Maybe you can use some control variable directly (which is associated with your textbox) instead of sender. Use the names that are set in Form Designer.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.