user1174834
user1174834

Reputation: 249

Reference an Object in a Method

How do I reference the student object created in the first button in my method below? I thought I could simply do "ref object student" but when I do it won't let me use any of the methods associated with the student class and simply says "object does not contain a definition for . . ."

Button:

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

Method:

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

Upvotes: 0

Views: 58

Answers (2)

user1968030
user1968030

Reputation:

You can use ref

ref Student student

Ref parameters are changed at the calling site. They are passed as references, not values. This means you can assign the parameter in the called method and have it also be assigned at the calling site.

Upvotes: 0

King King
King King

Reputation: 63387

Use this instead of ref object student:

ref Student student

Upvotes: 1

Related Questions