0

私は 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);
        }
    }

}
4

1 に答える 1

1

クラス Student に IComparable インターフェイスを実装させ、DateOfBirth フィールド (存在する場合) を並べ替えます。この例は AccountNum で機能しますが、DateOfBirth で変更するのは簡単です。

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");
    }
    ....

}
于 2013-03-20T19:52:24.190 に答える