2016-12-14 84 views
1

我对c#相当陌生,并且在列表中遇到困难。c#使用用户输入创建一个列表

我正在创建一个接受用户姓名,年龄和地址的应用程序,然后在用户单击“添加”按钮时将其存储在列表中。 我正在使用带有用户输入的文本框的GUI。

我已经做了一个客户类,并且不确定接下来要做什么。我遵循教程和其他问题,但似乎无法找到答案。

public class Customer 
{ 
    private string name; 
    private Int32 age; 
    private string address1; 
    private string address2; 
    private string address3; 


    public string Name 
    { 
     get 
     { 
      return name; 
     } 

     // if name is blank throw argument asking user for input 

     set 
     { 
      if (name == "") 
      { 
       throw new ArgumentException("Please enter your name"); 
      } 
      else 
      { 
       name = value; 
      } 
     } 
    } 

    public Int32 Age 
    { 
     get 
     { 
      return age; 
     } 

     set 
     { 
       age = value; 
     } 
    } 


    // get/set address 

    public string Address1 
    { 
     get 
     { 
      return address1; 
     } 

     set 
     { 
      if (address1 == "") 
      { 
       throw new ArgumentException("Please enter your address"); 
      } 
      else 
      { 
       address1 = value; 
      } 
     } 
    } 

    public string Address2 
    { 
     get 
     { 
      return address2; 
     } 

     set 
     { 
      if (address2 == "") 
      { 
       throw new ArgumentException("Please enter your adress"); 
      } 
      else 
      { 
       address2 = value; 
      } 
     } 
    } 

    public string Address3 
    { 
     get 
     { 
      return address3; 
     } 


     set 
     { 
      if (address3 == "") 
      { 
       throw new ArgumentException("Please enter your adress"); 
      } 
      else 
      { 
       address3 = value; 
      } 
     } 
    } 
+0

你没有显示关于使用列表中的任何代码,你所期待的。这就是我们需要帮助你的。 – krillgar

+0

研究通用列表:https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx。看看例子。在你的情况下,通用参数将是你的“客户”类。 –

+1

在'Name'属性设置器中,如果'name'为空则抛出异常。我假设你想抛出异常,如果'value'为空或空:'if(string.IsNullOrEmpty(value))throw new ArgumentException()...' –

回答

2

这是一个简单的Windows窗体窗体的例子,它会给你一个想法。基本上你想将客户列表存储在私有通用列表变量中。有关如何在C#中使用通用和非通用列表的更多信息,请参阅C#here

public partial class Form1 : Form 
{ 
    // Initialize private generic list where all customers will be stored at runtime 
    private List<Customer> _customers = new List<Customer>(); 

    private void buttonAddCustomer_Click(object sender, EventArgs e) 
    { 
     // It might be a good idea to add some validation logic before assigning the input values 
     var newCustomer = new Customer(); 
     newCustomer.Name = this.textBoxName.Text; 
     newCustomer.Age = Convert.ToInt32(this.textBoxAge.Text); 
     newCustomer.Address1 = this.textBoxAddress1.Text; 
     newCustomer.Address2 = this.textBoxAddress2.Text; 
     newCustomer.Address3 = this.textBoxAddress3.Text; 

     _customers.Add(newCustomer); 
    } 
} 

enter image description here

+0

谢谢你的帮助! – skittles

0

,我认为是在MakeItHappen()方法

using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 

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

     private void btn_Go_Click(object sender, EventArgs e) 
     { 
      MakeItHappen(); 
     } 

     private void MakeItHappen() 
     { 
      List<Customer> customerList = new List<Customer>();//initialize your List<Customer> 
      customerList.Add(new Customer { Name = txtbx_Name.Text, Address1 = txtbx_Address1.Text, Age = int.Parse(txtbx_Age.Text) });//add a record to it 
     } 
    } 



    public class Customer 
    { 
     private string name; 
     private Int32 age; 
     private string address1; 
     private string address2; 
     private string address3; 


     public string Name 
     { 
      get 
      { 
       return name; 
      } 

      // if name is blank throw argument asking user for input 

      set 
      { 
       if (name == "") 
       { 
        throw new ArgumentException("Please enter your name"); 
       } 
       else 
       { 
        name = value; 
       } 
      } 
     } 

     public Int32 Age 
     { 
      get 
      { 
       return age; 
      } 

      set 
      { 
       age = value; 
      } 
     } 


     // get/set address 

     public string Address1 
     { 
      get 
      { 
       return address1; 
      } 

      set 
      { 
       if (address1 == "") 
       { 
        throw new ArgumentException("Please enter your address"); 
       } 
       else 
       { 
        address1 = value; 
       } 
      } 
     } 

     public string Address2 
     { 
      get 
      { 
       return address2; 
      } 

      set 
      { 
       if (address2 == "") 
       { 
        throw new ArgumentException("Please enter your adress"); 
       } 
       else 
       { 
        address2 = value; 
       } 
      } 
     } 

     public string Address3 
     { 
      get 
      { 
       return address3; 
      } 


      set 
      { 
       if (address3 == "") 
       { 
        throw new ArgumentException("Please enter your adress"); 
       } 
       else 
       { 
        address3 = value; 
       } 
      } 
     } 


    } 
}