【发布时间】: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