引用方法中的对象

时间:2013-11-17 04:29:30

标签: c#

如何在下面的方法中引用第一个按钮中创建的学生对象?我以为我可以简单地做“ref object student”,但是当我这样做时,不会让我使用与学生类相关的任何方法,而只是说“对象不包含...的定义”。

按钮:

private void addStudentButton_Click(object sender, EventArgs e) //Add Student Button
    {
        string name = nameBox.Text;
        string course = courseBox.Text;
        bool NumTrue = true;
        decimal asgnScore;
        decimal mScore;
        decimal fScore;
        decimal itMJR; //IT Major Check

        student = new Student(name);
        student.SetCourse(course);

        VerifyNums(ref NumTrue, out asgnScore, out mScore, out fScore);

        if (NumTrue)
        {
            student.Assignment = asgnScore;
            student.Midterm = mScore;
            student.final = fScore;
            AddPoints(out itMJR);
            clearButton_Click(sender, e);
        }
        else
        {
            BadInput();
        }
    }

方法:

 private void CalculateGrades(ref object student, out decimal averageExamScore, out   decimal percentGrade, ref decimal AW, ref decimal TW, ref decimal itMJR)
    {

        asgnScore = ((asgnScore + itMJR) * AW);

        name = nameBox.Text; course = courseBox.Text;
        averageExamScore = (((fScore + itMJR) + (mScore + itMJR)) / 2);
        averageExamScore = averageExamScore * TW;
        percentGrade = averageExamScore + asgnScore;
    }

2 个答案:

答案 0 :(得分:1)

使用此代替ref object student

ref Student student

答案 1 :(得分:0)

您可以使用ref

ref Student student
  

参考站点更改参数参数。他们被传递为   引用,而不是价值观。这意味着您可以在中分配参数   调用方法,并在调用站点分配它。