C#标签显示不正确

时间:2014-12-09 19:31:00

标签: c# list class collections label

我正在创建一个注册应用程序,您可以在其中输入学生姓名,ID和gpa,它们都将存储在收藏列表中。即使学生注册,标签也显示为0。这是相关的代码。任何帮助表示赞赏。

namespace Lab09
{
class Student
{
    string name;
    int id;
    int intNumber;
    decimal gpa;

    public Student(string Name, int Id, decimal Gpa)
    {
        name = Name;
        id = Id;
        gpa = Gpa;
    }
    public string Name
    {
        set { name = value; }
        get
        {
            return name;
        }
    }
    public int Id
    {

        get
        {
            return id;
        }
    }
    public decimal Gpa
    {

        get
        {
            return gpa;
        }
    }
    public int Number
    {
        get
        {
            return intNumber;
        }
    }
}
}


namespace Lab09
{
public partial class Form1 : Form
{
    List<Student> listofStudents;
    int intCurrentStudent = 0;

    public Form1()
    {
        InitializeComponent();
        listofStudents = new List<Student>();
    }

private void btnRegister_Click(object sender, EventArgs e)
    {
        try
        {
            if (Decimal.Parse(txtGPA.Text) > 0 && Decimal.Parse(txtGPA.Text) <= 4)
            {
                if (txtName.Text != "")
                {
                    listofStudents.Add(new Student(txtName.Text, Int32.Parse(txtID.Text), Decimal.Parse(txtGPA.Text)));
                    intCurrentStudent = listofStudents.Count - 1;
                    txtName.Enabled = false;
                    txtID.Enabled = false;
                    txtGPA.Enabled = false;
                    btnRegister.Enabled = false;
                    if (listofStudents.Count > 1)
                    {
                        btnPrevious.Enabled = true;
                    }
                    displayNumStudents();
                }
            }
            else
            {
                MessageBox.Show("You must enter a name and GPA must be above 0 and less than or equal to 4");
            }
        }
        catch
        {
            MessageBox.Show("ID and GPA need to be numbers");
        }
    }

    private void displayNumStudents()
    {
        int NumOfStudents = 0;
        foreach (Student aStudent in listofStudents)
        {
            NumOfStudents += aStudent.Number;
        }
        lblNum.Text = NumOfStudents.ToString();
    }

2 个答案:

答案 0 :(得分:1)

您的int intNumber属性正在返回的字段Number,但您永远不会设置它。然后你用这个数字来计算你的学生,这是没有意义的。

我认为你想要注册的学生数:

private void displayNumStudents()
{
    int NumOfStudents = listOfStudents.Count();
    lblNum.Text = NumOfStudents.ToString();
}

答案 1 :(得分:0)

变量intNumber; 在您的学生班中永远不会分配,只能通过Number课程中的Student属性返回。

public int Number
{
    get
    {
        return intNumber;
    }
}

将新学生添加到集合中时,您需要更新此属性。

您可以修改Student构造函数以接受此参数并将其分配到那里:

 public Student(string Name, int Id, decimal Gpa, int studentNumber)
 {
        name = Name;
        id = Id;
        gpa = Gpa;
        intNumber = studentNumber;
 }

然后,您必须在创建新对象时将其传递给新的Student对象,然后将其添加到列表中,例如:

 string name = txtName.Text;
 int id = Int32.Parse(txtID.Text);
 decimal gpa = decimal.Parse(txtGPA.Text);
 int studentNumber = listofStudents.Count() + 1;

 Student student = new Student(name, id, gpa, studentNumber);

 listofStudents.Add(student);