How can I create an instance of a class that I can use is more than one place in c#

Posted by user1743574 on Stack Overflow See other posts from Stack Overflow or by user1743574
Published on 2012-10-13T15:28:23Z Indexed on 2012/10/13 15:37 UTC
Read the original article Hit count: 173

Filed under:
|
|

So I've been working on a class that details students of a university. I have one button that sets the details to a new instance of a class, and another to check if the student passed, through a method in my Class. The problem is that I create an instance of a class in the first button to add the values from what the user input, but I cannot use the second button to access the instance of the class created in the first button.

    private void button3_Click(object sender, EventArgs e)
    {

        Student student1 = new Student();
        label1.Text = student1.text();
        if (student1.hasPassed() == true)
        {
            passfailtextbox.Text = "Pass";
        }
        else
        {
            passfailtextbox.Text = "Fail";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Student student1 = new Student();
        student1.FirstName = firstnamebox.Text;
        student1.SecondName = secondnamebox.Text;
        student1.DateofBirth = DateTime.Parse(dobtextbox.Text).Date;
        student1.Course = coursetextbox.Text;
        student1.MatriculationNumber = int.Parse(matriculationtextbox.Text);
        student1.YearMark = double.Parse(yearmarktextbox.Text);

    }

    public Boolean hasPassed()
    {
        if (YearMark < 40)
        {

            return false;
        }
        else
        {
            return true;
        }

    }
    public string text()
    {
        return "Student" +
        " " + firstname +
        " " + secondname +
        " " + course +
        " " + dob.ToString() +
        " " + matriculationnumber.ToString() +
        " " + yearmark.ToString()
        ;
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about class