-1

当用户单击提交按钮时,我想将用户输入插入到数组中。这是我写的,但它似乎不起作用。该表单称为form1,它是它自己的类,文本框是textbox1。注意:我是编程新手。

//This is my array
private string[] texts = new string[10];

        public string[] Texts
        {
            get { return texts; }
            set { texts = value; }
        }

//I then attempt to insert the value of the field into the textbox
form1 enterDetails = new form1();
for(int counter = 0; counter<Texts.Length; counter++)
{
texts[counter]=enterDetails.textbox1.Text;
}
4

1 回答 1

0

你在这里犯了一些愚蠢的错误:

  1. 在 Texts 属性的设置器中,您应该说

    texts = value;
    

    代替guestNames = value;

  2. 您不需要创建 form1 的新实例,因为您上面编写的所有代码都已经在 form1 类中。如果没有,则尝试获取 form1 的相同实例。

  3. 没有必要,但您应该设置属性而不是字段。

    代替

    texts[counter] = .......
    

    Texts[counter] = ..........
    

因此,您的完整代码应如下所示:

    public form1() //Initialize your properties in constructor.
    {
        Texts = new string[10]
    }

    private string[] texts;

    public string[] Texts
    {
        get {return texts; }
        set { texts = value; }
    }

    for(int counter = 0; counter<Texts.Length; counter++)
    {
        Texts[counter]=this.textbox1.Text;
    }
于 2014-11-23T17:02:44.797 回答