【问题标题】:C# Arrays Help Needed Please需要 C# 数组帮助
【发布时间】:2013-03-20 19:37:41
【问题描述】:

我是使用 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);
        }
    }

}

【问题讨论】:

  • 你在哪里定义了bc
  • 您的实际问题是什么?请发布任何编译器错误或异常消息。
  • 您的问题的症状是什么?错误信息?意外行为?请帮助我们帮助您!
  • 您说学生应该按出生日期排序,但您的班级学生没有 DateOfBirth 属性
  • 哇,回复很快,谢谢!我没有收到任何错误消息,我只是想找到一种方法来创建一个数组并将它们列在富文本框中。我没有添加我所有的代码,但请放心 DOB 已定义。我的编码中没有定义“bc”。

标签: c# arrays visual-studio class


【解决方案1】:

让您的学生类实现 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");
    }
    ....

}

【讨论】:

  • 感谢您的回答,我正在寻找来自 textBox1、textBox2 等的输入...例如 bc[1] = new Student(textBox1.Text, textBox2.Text, "福特”); bc[2] = new Student(textBox1.Text, textBox2.Text, "白色");
  • 怎么样,如果用户通过文本框添加记录,我是否可以将其添加到 txt 文件中,然后读取文件并通过富文本框按顺序显示?
  • 通常这个任务是通过两种形式来完成的。第一个显示学生记录(以网格、列表视图或其他形式),第二个表单显示单个学生的数据,用户可以在其中更新呈现的值。当第二种形式被确认时,数据被复制回 bc 数组中正确的索引处。但这在这里解释有点宽泛。我建议将您的任务分成更小的步骤,并为每个步骤提出适当的问题
  • 当然,我希望用户在文本框中输入信息,该信息将被存储。在 10 名学生输入他们的信息后,我想在 GUI 中按 DOB 顺序显示所有 10 名学生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2013-12-08
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 2016-03-31
相关资源
最近更新 更多