Click here to Skip to main content
15,910,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, I am learning C#, i have a simple MathClass would like to use in Winform.
it works but something not clear.

Is it Okay or conventional to instantiate at here?

private void button1_Click(object sender, EventArgs e)
     {
         MathClass obj = new MathClass(x, y);
         textBox3.Text = obj.Add().ToString();
     }



class MathClass
    {
        private int _x;
        private int _y;

      public MathClass(int x,int y)
        {
            _x = x;
            _y = y;
        }
      public int Add()
        {
            return _x + _y;
        }
    }


What I have tried:

I try to instantiate :
1. Outside the button1_Click method, but inside partial class Form1, it show error.
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       int x = 0;
       int y = 0;

       MathClass obj = new MathClass(x, y);


2. inside
private void InitializeComponent()

no error, but obj cannot use inside Form1 anymore.
Posted
Updated 11-May-22 11:39am

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:
C#
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.
 
Share this answer
 
I think your design needs re-working: there should be no need to instantiate the class more than once in order to invoke its methods:
class MathClass
{
        public MathClass(){}
        
        public int Add(int x, int y))
        {
            return x + y;
        }

        public int Sub(int x, int y))
        {
            return x - y;
        }
}
Use: I assume you need to get your x,y values from two TextBoxes:
// in the Form
MathClass mathClassInstance = new MathClass();
'
// in the Buttom Click Event
private void button1_Click(object sender, EventArgs e)
{
    int x, y;
    
    if (Int32.TryParse(textBox1.Text, out x))
    {
        if (Int32.TryParse(textBox2.Text, out y))
        {
            textBox3.Text = mathClassInstance.Add(x, y).ToString();
            return;
        }
    }

    throw new ArgumentException("Bad input");
}
 
Share this answer
 
Comments
Richard Deeming 17-May-18 12:14pm    
For methods which don't use any instance members, there should be no need to instantiate the class at all to invoke them. :)
static class MathClass
{
    public static int Add(int x, int y) => x + y;
    public static int Sub(int x, int y) => x - y;
}
BillWoodruff 17-May-18 16:55pm    
@richarddeeming

Hi Richard, I thought about showing the OP a static class, and about showing a class not defined as static which exposes static methods. I decided to keep it simpler based on my sense of the level the OP is at.

cheers, Bill
You can't pass parameters outside a method. Declare it where you are, outside a method, and create it in the constructor
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900