2013-03-20 106 views
0

我是使用C#编程的初学者,我的讲师给了我们一个棘手的项目。 我已经设法完成除了......数组之外的所有数据!C#数组帮助需要请

长话短说,我有5个文本框,所有这些文本框都从用户那里接受输入。这个信息是在丰富的文本框中显示被存储到一个数组,然后按顺序列出(出生顺序的日期),我列出我已经成功下方做代码:

private void button2_Click(object sender, EventArgs e) 
{ 
    { 
     bc[0] = new Student(); 
     bc[1] = new Student(Convert.ToInt32(textBox1.Text), "Mary", "Ford"); 
     bc[2] = new Student(1254, "Andrew", "White"); 
     bc[3] = new Student(1256, "Liam", "Sharp", " "); 
     bc[4] = new Student(1266, "Michael", "Brown", " "); 

     for (int i = 0; i < 5; i++) 
     { 
     string bcString = bc[i].studentToString() + "\r\n"; 
     richTextBox1.AppendText(bcString); 
     } 
    } 
}  

CLASS "Student": 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Assignment_2 
{ 
    class Student 
    { 
     private int accountNum; 
     private string firstName; 
     private string lastName; 
     private string balance; 

     // first constructor 
     public Student() 
     { 
      accountNum = 0; 
      firstName = ""; 
      lastName = ""; 
      balance = ""; 
     } 

     // second constructor 
     public Student(int accValue, string firstNameVal, string lastNameVal) 
     { 
      accountNum = accValue; 
      firstName = firstNameVal; 
      lastName = lastNameVal; 
      balance = ""; 
     } 

     // third constructor 
     public Student(int accValue, string firstNameVal, 
          string lastNameVal, string balanceValue) 
     { 
      accountNum = accValue; 
      firstName = firstNameVal; 
      lastName = lastNameVal; 
      balance = balanceValue; 
     } 

     public int AccountNum 
     { 
      get 
      { 
       return accountNum; 
      } 

      set 
      { 
       accountNum = value; 
      } 
     } 

     public string FirstName 
     { 
      get 
      { 
       return firstName; 
      } 

      set 
      { 
       firstName = value; 
      } 
     } 

     public string studentToString() 
     { 
      return (Convert.ToString(accountNum) + " " + firstName + 
        " " + lastName + " " + balance); 
     } 
    } 

} 
+1

你上哪儿去定义'bc'? – 2013-03-20 19:38:52

+2

你的实际问题是什么?请发布任何编译器错误或异常消息。 – 2013-03-20 19:39:29

+0

你的问题有哪些症状?错误讯息?意外的行为?请帮助我们帮助你! – Sebivor 2013-03-20 19:40:17

回答

1

让你class Student实现IComparable接口,然后对字段DateOfBirth进行排序(如果存在)。此示例适用于AccountNum但应该是微不足道的有出生日期

Student[] bc = new Student[5]; 

bc[0] = new Student(); 
bc[1] = new Student(9999, "Mary", "Ford"); 
bc[2] = new Student(1254, "Andrew", "White"); 
bc[3] = new Student(1256, "Liam", "Sharp", " "); 
bc[4] = new Student(1266, "Michael", "Brown", " "); 

// Here the sort on the AccountNum 
Array.Sort(bc); 

// A StringBuilder instead of the RichTextBox for testing....  
StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < 5; i++) 
{ 
    string bcString = bc[i].studentToString() + "\r\n"; 
    sb.Append(bcString); 
} 
Console.WriteLine(sb.ToString()); 

类学生改变:(只是一部分了IComparable)

class Student : IComparable 
{ 
    ..... 


    public int CompareTo(object obj) 
    { 
     if (obj == null) return 1; 

     Student otherStudent = obj as Student; 
     if (otherStudent != null) 
      return this.accountNum.CompareTo(otherStudent.AccountNum); 
     else 
     throw new ArgumentException("Object is not a Student"); 
    } 
    .... 

} 
+0

谢谢你回答,我正在寻找的是从textBox1,textBox2等输入...例如bc [1] =新学生(textBox1.Text,textBox2.Text,“福特”); bc [2] =新生(textBox1.Text,textBox2.Text,“White”); – NoobProgrammer 2013-03-20 19:58:19

+0

那么如果用户通过文本框添加记录,我可以将它添加到txt文件中,然后读取文件并通过富文本框按顺序显示它? – NoobProgrammer 2013-03-20 20:01:46

+0

通常这个任务是通过两种形式完成的。第一个显示学生记录(在网格,列表视图或其他内容中),第二种形式显示单个学生的数据,用户可以更新所呈现的值。当第二个表单被确认时,数据被复制回正确索引的bc数组。但这里有点宽泛的解释。我建议在较小的步骤中划分你的任务,并为每个步骤询问适当的问题 – Steve 2013-03-20 20:05:24