Don't add it to InitializeComponent: that is to prepare controls and such like that are managed by the Visual Studio Designer - generally, you don't need to edit that at all.
This is perfectly valid:
private void button1_Click(object sender, EventArgs e)
{
MathClass obj = new MathClass(x, y);
textBox3.Text = obj.Add().ToString();
}
And we instantiate objects all the time:
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
SaveFile(sfd.FileName);
}
}
but the naming could use some work: "obj" is not a good name for anything as it implies you don't know what the type is:
MathClass mc = new MathClass(x, y);
textBox3.Text = mc.Add().ToString();
You can't do it like this outside a method:
public Form1()
{
InitializeComponent();
}
int x = 0;
int y = 0;
MathClass obj = new MathClass(x, y);
because the creation of the MathClass object relies on other variables (x and y) and there is no guarantee that they have been initialized when it tries to create the instance. Think about it - what if you did this:
int x = y + 2;
int y = z - 3;
int z = 9;
That requires z to have been created and initialized before x, which isn't obvious unless you read y first!
Class level initializations must be simple: anything more complex needs to be in the form constructor which ios only executed once the "basic variable" init is complete.