简体   繁体   English

C# 如何将 textbox.text 作为对象(在类中)变量导入? Windows Forms 应用程序

[英]C# How to import textbox.text as object(in class) variable? Windows Forms app

I need to import the text that is inside "Parallel_quantity" textbox into the class object, but i get this error: CS0120: An object reference is required for the nonstatic field, method, or property我需要将“Parallel_quantity”文本框内的文本导入 class object,但我收到此错误:CS0120: An object 引用,非静态属性字段需要引用

Below is my code:下面是我的代码:

  class production_ordered
    {
        string production_time_obj;
        int parallel_ordered;
        int linear_ordered;
        string type;


       
        public void Ordered_prd()
        {
            production_ordered MyObj1 = new production_ordered();
            bool par;
            int a;
            par = int.TryParse(Parallel_quantity.Text, out a);
            MyObj1.parallel_ordered = a;
        }

    }

How to deal with it?如何处理? Thanks in advance.提前致谢。

Parallel_quantity is a property of the Form it is placed on (more specifically - the class created for the Form). Parallel_quantity 是它所在的 Form 的一个属性(更具体地说 - 为 Form 创建的 class)。 It does not exist in the context of the production_ordered class.它在production_ordered class 的上下文中不存在。

To deal with it, you have several options.要处理它,您有多种选择。 The simplest suggestion would be to move the Ordered_prd method outside of the production_ordered class, inside of your Form class (eg Form1).最简单的建议是将Ordered_prd方法移到production_ordered class 之外,在您的表格 class(例如 Form1)内部。 You would also need to change你还需要改变

int parallel_ordered;

into进入

public int parallel_ordered;

in order to make it accessible in the method.为了使其在方法中可访问。

Resulting code would be something like this结果代码将是这样的

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void Ordered_prd()
    {
        production_ordered MyObj1 = new production_ordered();
        bool par;
        int a;
        par = int.TryParse(Parallel_quantity.Text, out a);
        MyObj1.parallel_ordered = a;
    }
}

class production_ordered
{
    string production_time_obj;
    int linear_ordered;
    string type;

    public int parallel_ordered;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM