-1

我想在用户单击提交按钮时将用户输入插入到数组中。 这是我写的,但它似乎并没有工作。该表单称为form1,它是它自己的类,文本框是textbox1。注意:我是编程方面的新手。如何从C#中的文本框将输入插入到数组中#

//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; 
} 
+0

什么是不工作?发生什么事? – 2014-11-23 16:54:12

+0

您好像已经粘贴了2或3个代码片段,但不可能知道发生了什么 – dcastro 2014-11-23 16:59:15

+0

这可能不能解决您的问题,但仍然是:一个属性,其setter设置的变量与getter检索到的变量不同是*巨大的*代码气味 – dcastro 2014-11-23 17:00:31

回答

0

你已经在这里做了一些愚蠢的错误:

  1. 在文本属性的制定者,你应该说

    texts = value; 
    

    ,而不是guestNames = value;

  2. 你不需要创建form1的新实例,因为上面写的所有代码都是alrea dy在form1类中。如果没有,则尝试获取form1的同一个实例。

  3. 没有必要,但你应该设置你的财产不是领域。

    更换

    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; 
    } 
相关问题