【问题标题】:Method of second class doesn`t want to work in main class第二类的方法不想在主类中工作
【发布时间】:2020-03-18 15:04:13
【问题描述】:

伙计们!我有个问题。我的方法不想在主类 Form1 中工作。 言归正传。我有主课 Form1

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

        public void GetLengthFirst_Click(object sender, EventArgs e)
        {
            string result = Microsoft.VisualBasic.Interaction.InputBox("Input elem:");
            FirstArray.Text = result;

            // WHEN I CALL METHOD FROM MY CLASS Array IT DOESNT WORK
            Array _arr = new Array();
            _arr.Masstrickt(result);

            /* BUT WHEN I CALL PROPERTIES OF THIS CLASS FOR EXMPL
               ReturnSecond = "12345";
               IT WORK*/
        }

        public string ReturnSecond
        {
            get { return SecondArray.Text; }
            set { SecondArray.Text = value; }
        }
    }

二等:

class Array
    {
        public void Masstrickt(string x)
        {
            Form1 frm = new Form1();
            frm.ReturnSecond = x; 
        }
    }

对不起我的语法。我不是母语人士

【问题讨论】:

  • Form1 frm 在您的Array 类中不是您当前Form1 的真实实例。您可以将void Masstrickt(string x) 更改为string Masstrickt(string x)ReturnSecond = _arr.Masstrickt(result);
  • 好吧,也许你需要详细说明“什么不起作用”。

标签: c# winforms oop


【解决方案1】:

您想将Masstrickt 的结果传递给您的表单,因此您需要将您的函数返回类型从void 更改为string

如果您在Form1 类之外声明 Array,请将其设置为 public

// public keyword would make Array visible to Form1 
public class Array
    {
        // Return type is string
        public string Masstrickt(string x)
        {
             return x;
        }
    }

Form1

  public void GetLengthFirst_Click(object sender, EventArgs e)
        {
            string result = Microsoft.VisualBasic.Interaction.InputBox("Input elem:");
            FirstArray.Text = result;

            Array _arr = new Array();
            ReturnSecond = _arr.Masstrickt(result);
            // ReturnSecond is the content of result 
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    相关资源
    最近更新 更多