简体   繁体   中英

How to ref a variable value inside a button_click method

I have a class that constructs a special kind of message boxes. In that class one of the arguments is a variable which i'm refering in the constructor However I need that variable to be recognized out of the constructor, more specifically when i click a button.

I took a look at methods behavior and noticed that in order for a method to recognize a value from a variable i had to refer it like this.

static void SetString1(ref string value)
{
if (value == "cat") // Test parameter value
{
    Console.WriteLine("Is cat");
}
value = "dog"; // Assign parameter to new value
}

I wanna do the same, but in a button click method, however if i try to refer the variable ´variavelcaixa´, it will give me "No overload for buttonRight_click matched delegate System.eventhandler". What does this means, and how should i sucessfully refer the variable?

private void buttonRight_Click(object sender, System.EventArgs e, ref int variavelcaixa)
    {
        if (checkBox1.Checked == true)
        { variavelcaixa = 1; }
        else { variavelcaixa = 0; }
    }

EDIT: The code i have in the special message box class is as follows:

     public partial class BetterDialog : Form
   {
    public int variavelcaixa;
 static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation,
        string leftButton, string rightButton, Image iconSet, ref int variavelcaixa)
    {
        using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,
            rightButton, iconSet, ref variavelcaixa))
        {
            DialogResult result = dialog.ShowDialog();
            return result;
        }
    }

    /// <summary>
    /// The private constructor. This is only called by the static method ShowDialog.
    /// </summary>
    private BetterDialog(string title, string largeHeading, string smallExplanation,
        string leftButton, string rightButton, Image iconSet, ref int variavelcaixa)
    {
        this.Font = SystemFonts.MessageBoxFont;
        this.ForeColor = SystemColors.WindowText;

        InitializeComponent();
  //A bunch of graphic design
    }

outside of the constructor there is the button click method

    private void buttonRight_Click(object sender, System.EventArgs e)
    {
        if (checkBox1.Checked == true)
        { variavelcaixa = 1; }
        else { variavelcaixa = 0; }
    }

On the main class i simply add the ref variavelcaixa, with a specific variable attached to the message box object

MsgBoxCheck.MessageBox dlg = new MsgBoxCheck.MessageBox();
                string icone = "C:\\warning.png";
                DialogResult result = BetterDialog.ShowDialog("Alert",
      "main message in message box",
      "some secondary message",
      "", "Ok", System.Drawing.Bitmap.FromFile(icone), ref Variables.checkbox53naomostrarnovamente);

The button click event handler has to have the function signature (object sender, System.EventArgs e) MSDN Reference . That is why when you try to make your handler take in the third paramter, there is an error.

One way to work around this is to make the variable variavelcaixa accessible to the handler method. You could do this by declaring it as a global variable in your class. You can then assign to it in the constructor, as well as in the event handler method. Note that once you do this, you should not pass in ref int variavelcaixa to the event handler method.

Basically, once you call showDialog and pass in a ref parameter, you need to assign it to your class variable variavelcaixa in the method body. ref Variables.checkbox53naomostrarnovamente will refer to checkbox53naomostrarnovamente , not your variable. Therefore, change your code to this:

static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation, string leftButton, string rightButton, Image iconSet, ref int p_variavelcaixa)
{
    using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,
        rightButton, iconSet, ref p_variavelcaixa))
    {
        variavelcaixa = p_variavelcaixa;
        DialogResult result = dialog.ShowDialog();
        return result;
    }
}

Note that I have renamed your ref parameter to p_variavelcaixa to help clear up the confusion. You may want to take a look at this demo too for understanding the variable - parameter thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM