首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >保存学生成绩

保存学生成绩
EN

Code Review用户
提问于 2012-05-12 14:10:22
回答 2查看 1.1K关注 0票数 4

我有一个程序,它以学生的名字,姓和分数,并保存它。在检查textbox中是否包含信息时,我使用了许多if语句。有更干净的方法吗?

代码语言:javascript
运行
复制
public void CheckData()
    {
        if (first1.Text != empty & score1.Text != empty & last1.Text != empty &
            first2.Text != empty & score2.Text != empty & last2.Text != empty &
            first3.Text != empty & score3.Text != empty & last3.Text != empty &
            first4.Text != empty & score4.Text != empty & last4.Text != empty &
            first5.Text != empty & score5.Text != empty & last5.Text != empty)
        {
            student1 = new Student(first1.Text, last1.Text, Convert.ToInt32(score1.Text));
            student2 = new Student(first2.Text, last2.Text, Convert.ToInt32(score2.Text));
            student3 = new Student(first3.Text, last3.Text, Convert.ToInt32(score3.Text));
            student4 = new Student(first4.Text, last4.Text, Convert.ToInt32(score4.Text));
            student5 = new Student(first5.Text, last5.Text, Convert.ToInt32(score5.Text));

            if (first6.Text != empty & score6.Text != empty & last6.Text != empty)
            {
                student6 = new Student(first6.Text, last6.Text, Convert.ToInt32(score6.Text));

                if (first7.Text != empty & score7.Text != empty & last7.Text != empty)
                {
                    student7 = new Student(first7.Text, last7.Text, Convert.ToInt32(score7.Text));

                    if (first8.Text != empty & score8.Text != empty & last8.Text != empty)
                    {
                        student8 = new Student(first8.Text, last8.Text, Convert.ToInt32(score8.Text));

                        if (first9.Text != empty & score9.Text != empty & last9.Text != empty)
                        {
                            student9 = new Student(first9.Text, last9.Text, Convert.ToInt32(score9.Text));

                            if (first10.Text != empty & score10.Text != empty & last10.Text != empty)
                            {
                                student10 = new Student(first10.Text, last10.Text, Convert.ToInt32(score10.Text));

                                if (first11.Text != empty & score11.Text != empty & last11.Text != empty)
                                {
                                    student11 = new Student(first11.Text, last11.Text, Convert.ToInt32(score11.Text));

                                    if (first12.Text != empty & score12.Text != empty & last12.Text != empty)
                                    {
                                        student12 = new Student(first12.Text, last12.Text, Convert.ToInt32(score12.Text));

                                        if (first13.Text != empty & score13.Text != empty & last13.Text != empty)
                                        {
                                            student13 = new Student(first13.Text, last13.Text, Convert.ToInt32(score13.Text));

                                            if (first14.Text != empty & score14.Text != empty & last14.Text != empty)
                                            {
                                                student14 = new Student(first14.Text, last14.Text, Convert.ToInt32(score14.Text));

                                                if (first15.Text != empty & score15.Text != empty & last15.Text != empty)
                                                {
                                                    student15 = new Student(first15.Text, last15.Text, Convert.ToInt32(score15.Text));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        else
        {
            MessageBox.Show("You need a Minimum of 5 Students");
        }
    }

变量

代码语言:javascript
运行
复制
Student student1;
Student student2;
Student student3;
Student student4;
Student student5;
Student student6;
Student student7;
Student student8;
Student student9;
Student student10;
Student student11;
Student student12;
Student student13;
Student student14;
Student student15;
string empty = "";

public class Student
{
    string First { get;  set; }
    string Last { get;  set; }
    int Score { get;  set; }

    public Student(string first, string last, int score)
    {
        first = First;
        last = Last;
        score = Score;
    }
}
EN

回答 2

Code Review用户

回答已采纳

发布于 2012-05-12 18:43:22

复制问题可以使用自定义UserControl来解决,以封装用于编辑Student的控件以及验证。

要定义控件,请执行以下操作:

  • 右键单击解决方案资源管理器中的项目
  • 选择添加-> UserControl..。
  • 输入控件的名称(例如: StudentControl)
  • 按“添加”按钮

在添加用户控件之后,您可以像其他任何形式一样设计它。添加单个学生所需的所有控件:三个文本框(例如,txtFirstNametxtLastNametxtScore)以及标签(如果您认为需要的话)。

建立你的解决方案。

然后,在您的表单上,将15 StudentControls (您应该在工具箱中找到它们)放置在45个文本框中。

现在,对于代码部分。

控件需要能够从控件中读取学生,因此在StudentControl类中添加以下代码:

代码语言:javascript
运行
复制
public Student GetStudent()
{
    if (txtFirstName.Text == String.Empty || 
        txtLastName.Text == String.Empty || 
        txtScore.Text == String.Empty)
        return null;

    return new Student(txtFirstName.Text, txtLastName.Text, int.Parse(txtScore.Text));
}

现在您的CheckData()方法可以简化如下:

代码语言:javascript
运行
复制
public void CheckData()
{
    var students = Controls.OfType<StudentControl>()
        .Select(studentControl => studentControl.GetStudent())
        .Where(student => student != null)
        .ToList();

    if(students.Count<5)
        MessageBox.Show("You need a Minimum of 5 Students");
}

通过这样做,我们获得了一些优势:

  • 我们减少了复制。如果有一天你必须为一个学生添加一个新的字段,你只需要添加一次,而不是15次。另外,如果明天您需要表单来容纳10或20个学生,这是一个在表单设计器中执行的简单操作,没有任何代码含义。
  • 我们已经分离了一些关注点:现在用户控件处理编辑单个学生的逻辑,而表单只知道它有一些用于构造StudentControl对象的Student实例。
  • 代码变得更小了。这意味着任何人都可以更快地阅读和理解它,从而获得更好的可维护性。
票数 8
EN

Code Review用户

发布于 2012-05-12 18:04:22

首先,筑巢是很可怕的!您可以恢复ifs的条件,并使用返回来避免使用else

代码语言:javascript
运行
复制
public void BadNesting() {
  if(cond1) {
    //...
    if(cond2) {
      // ...
      if(cond3) {
        //...
      }
    }
  }
}

public void Better() {
  if(!cond1) {
    return;
  }
  // ...
  if(!cond2) {
    return;
  }
  // ...
  if(!cond3) {
    return;
  }
  // ...
}

你也经常重复你的代码。我们可以使用这些建议来使用DataGridView,并将代码神奇地扩展到您可能遇到的许多学生:

代码语言:javascript
运行
复制
List<Student> Students;
public void CheckData(int minStudents = 5) {
    Students = new List<Student>();
    for (int i = 0; i < gridView.Rows.Count; i++) {
        var row = gridView.Rows[i];
        TextBox firstName = (TextBox)row.Controls[0];
        TextBox lastName = (TextBox)row.Controls[1];
        TextBox scoreTB = (TextBox)row.Controls[2];

        string name = firstName.Text;
        string familyName = lastName.Text;
        int score;
        if (string.IsNullOrEmpty(name)
            || string.IsNullOrEmpty(familyName)
            || !int.TryParse(scoreTB.Text, out score)
            ) {
            // Will not parse any more students.
            if (i < minStudents) {
                MessageBox.Show(string.Format("You need a minimum of {0} students", minStudents));
            }
            break;
        }
        var student = new Student(name, familyName, score);
        Students.Add(student);
    }
}
票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/11718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档