在winform中实例化的地方? [英] Where to instantiate in winform?

查看:71
本文介绍了在winform中实例化的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C#,我有一个简单的MathClass想在Winform中使用。

它有效,但有些不清楚。



在这里实例化是好的还是传统的?



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;
        }
    }





我的尝试:



我尝试实例化:

1.在button1_Click方法之外,但在部分类Form1中,它显示错误。



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.内部



2. inside

private void InitializeComponent()



没有错误,但obj不能在Form1中使用。


no error, but obj cannot use inside Form1 anymore.

推荐答案

不要将它添加到InitializeComponent:那就是准备控件等等由Visual Studio Designer管理 - 通常,您根本不需要编辑它。

这完全有效:
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);
         }
     }

但是命名可能会使用一些工作:obj不是一个好名字,因为它暗示你不知道它是什么类型:

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);

因为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;

这需要z在x之前创建并初始化,除非你先读取y,否则这是不明显的!

类级别初始化必须简单:任何更复杂的东西都需要在表单构造函数中,ios只执行一次基本变量init is complete。

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.


我认为你的设计需要重新工作:不需要多次实例化来调用它方法:
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;
        }
}

使用:我假设您需要从两个TextBox中获取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");
}


您不能在方法之外传递参数。在方法之外声明它的位置,并在构造函数
You can't pass parameters outside a method. Declare it where you are, outside a method, and create it in the constructor


这篇关于在winform中实例化的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆