Skip to content

Passing variables

Ian

Asked Nov 16, 2005, 6:15 AM EST2 answers
Share
LinkedInX
okay, I've got a program where I get the coordinates of where the mouse clicked in the code for the form:

protected override void OnMouseDown(MouseEventArgs e)
{
           int x, y;
           x = e.X;
           y = e.Y;
           base.OnMouseDown (e);
}

and I want to pass the values of x and y to a separate class and method. How do I do that?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I'm not sure of exactly what you are asking (as you were not very specific), but if I understand you correctly, the best solution is probably this: Make x and y private variables of the main form. private int x; private int y; Next, make public variables X and Y of the form to return the value of the private variables. public int X { get { return x; } } public int Y { get { return y; } } Next, inside your class, make the function take as a parameter your main form. public void showCoord(Form1 mainForm) { MessageBox.Show("X is " + mainForm.X + ", and Y is " + mainForm.Y + ".", "Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } Next, when you call the function from your main form, pass the main form as the parameter. private void button2_Click(object sender, System.EventArgs e) { Class1 c1 = new Class1(); c1.showCoord(this); } Anyway, good luck with your project. PS to all C# pros, I have a graphics question in the GDI forum that I'd like answered. :)
    0
  • 0
    Someone has to tell me how to format my posts on this forum, because it's not recognizing the return key at all...
    0