0

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);
ng80092b
  • 621
  • 1
  • 9
  • 24
  • You aren't actually assigning to the variable even when you pass in as `ref`. Having the same name in your parameter and variable won't automatically assign value. – shree.pat18 Dec 14 '14 at 07:03
  • @shree.pat18 So how do i link them? – ng80092b Dec 14 '14 at 07:06
  • Assign to the variable in `ShowDialog`! – shree.pat18 Dec 14 '14 at 07:07
  • But ShowDialog already has ref int variavelcaixa as an argument – ng80092b Dec 14 '14 at 07:08
  • But it isn't the same as your class variable! The one in your method signature is only a method parameter. – shree.pat18 Dec 14 '14 at 07:11
  • 1
    @shree.pat18 Can you provide more detailed information? I'm not getting it, sorry :\ – ng80092b Dec 14 '14 at 07:16
  • What is the purpose of all this? Why are you trying so badly to pass this additional ref parameter to the function? – t3chb0t Dec 14 '14 at 07:47
  • @t3chb0t Because it would allow me to create multiple messagebox objects, each with a different variable that i will use to "not show again" – ng80092b Dec 14 '14 at 16:22
  • Anyways i solved it without all the hassle.. I created a `public int variavelcaixa;` at the beginning of the messagebox class, and stated that everytimes it starts, variavelcaixa = 0;. After i create the message box, a variable assigned to it will take variavelcaixa value, and so it will know if the checkbox was checked. – ng80092b Dec 14 '14 at 19:37

1 Answers1

1

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.

shree.pat18
  • 21,449
  • 3
  • 43
  • 63
  • That's the easy way out, and i tried to do it, but even with that, he wouldn't recognize the variable value, although the code ran without bugs. I'm trying to add that variable as an extra argument in the following code http://www.dotnetperls.com/customized-dialog – ng80092b Dec 14 '14 at 06:44
  • There's a ton of code on that link, which one are you referring to? Also, you might want to post the code you have written with my suggested approach, so that we can see what's wrong with it. – shree.pat18 Dec 14 '14 at 06:46
  • I added the code in a edition, and in the messagebox class the variavelcaixa is made public of course :) – ng80092b Dec 14 '14 at 06:58
  • aw yes, but now it gets me another error, on `variavelcaixa = p_variavelcaixa;` "An object reference is required for the non-static field, method, or property 'DotNetPerls.BetterDialog.variavelcaixa'" – ng80092b Dec 14 '14 at 07:32
  • Ya well you need to access it as `object.variable` unless you make it a static variable. – shree.pat18 Dec 14 '14 at 07:33
  • Ok it runs without errors, but the value 1 doesn't reach the `Variables.checkbox53naomostrarnovamente` it stays 0, even when the checkbox is clicked and i click the buttonRight – ng80092b Dec 14 '14 at 07:39
  • It's almost working perfectly, but i had to change the code you gave me, it's `P_variavelcaixa = variavelcaixa;` and not otherwise, and it will pass the value onto `Variables.checkbox53naomostrarnovamente`. However it only does so the second time the message opens and i click the buttonRight. The code is missing something, can you help? – ng80092b Dec 14 '14 at 07:55