繁体   English   中英

在方法中引用对象

[英]Reference an Object in a Method

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

按钮:

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;
    }

使用它代替ref object student

ref Student student

您可以使用ref

ref Student student

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM