1

I would like to pass int _iD to my button click event. For now, I've passed my value to textbox and use it in button click. Is there anyway to bypass the textbox?

public MifarePasswordForm(int _iD)
{
    InitializeComponent();
    int iD = _iD;
    textBox1.Text += iD;
}

Button click event

private void btnOK_Click(object sender, EventArgs e)
{
    byte Oid = Convert.ToByte(textBox1.Text);
}
2
  • make a public property and use it. Commented May 26, 2016 at 6:37
  • you mean change the button click event to public? Commented May 26, 2016 at 6:39

3 Answers 3

1

Make a private property if both methods exist in single class else make public property and use it,

public int nID;

public MifarePasswordForm(int _iD)
{
    InitializeComponent();
    nID = _iD;
    textBox1.Text += iD;
}

Button click event

private void btnOK_Click(object sender, EventArgs e)
{
    byte Oid = Convert.ToByte(nID);
}
Sign up to request clarification or add additional context in comments.

Comments

1

What you currently do is to make your iD a local variable (that is, in the method/constructor scope):

public MifarePasswordForm(int _iD)
{
    InitializeComponent();
    int iD = _iD; //here it is, the iD is in the constructor/method scope, 
                  //it cannot be accessed outside of the scope
    textBox1.Text += iD;
}

You should make your iD a private field in the class scope rather than in the method scope:

private int iD; //declare here
public MifarePasswordForm(int _iD)
{
    InitializeComponent();
    iD = _iD; //don't declare here
    textBox1.Text += iD;
}

So that you can use it like this:

private void btnOK_Click(object sender, EventArgs e)
{
   byte Oid = (byte)iD;
 }

Comments

1

You should declare this variable as class level variable:

public class MifarePasswordForm
{
    public int iD {get;set;} // or private field

    public MifarePasswordForm(int _iD)
    {
        InitializeComponent();
        iD = _iD; // here you don't create, only use
        textBox1.Text += iD;
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
       //inside this method you can use this variable

       byte Oid = Convert.ToByte(iD);
    }

    //other code
}

Now you can use this variable in any method of this class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.