Rusty Shackleford Rusty Shackleford - 1 month ago 18
C# Question

MessageShow in another class?

This is my first time trying to implement separate class into form class, rather than just having it all in the form class. How do I go about connecting the two? When the button is clicked, I wanted to be able to have it show the letter grade with MessageBox.Show.

Thanks.

Form1.cs

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

public void button1_Click(object sender, EventArgs e)
{
Grade Nested = new Grade();

}
}


Class.cs

class Grade
{
private int number;

public int Number
{
get { return number; }

set { number = value; }
}

public static int Nested(int grade)
{

if (grade >= 93)
{
MessageBox.Show("A");
}

else if (grade >= 90 && grade <= 92)
{
MessageBox.Show("A-");
}

else if (grade >= 87 && grade <= 89)
{
MessageBox.Show("B+");
}

else if (grade >= 83 && grade <= 86)
{
MessageBox.Show("B");
}

else if (grade >= 80 && grade <= 82)
{
MessageBox.Show("B-");
}

else if (grade >= 77 && grade <= 79)
{
MessageBox.Show("C+");
}

else if (grade >= 73 && grade <= 76)
{
MessageBox.Show("C");
}

else if (grade >= 70 && grade <= 72)
{
MessageBox.Show("C-");
}

else if (grade >= 67 && grade <= 69)
{
MessageBox.Show("D+");
}

else if (grade >= 63 && grade <= 66)
{
MessageBox.Show("D");
}

else if (grade >= 60 && grade <= 62)
{
MessageBox.Show("D-");
}

else if (grade <= 59)
{
MessageBox.Show("F");
}
return;
}
}

Answer

You have made the Nested method static, so you can just call it like this:

public void button1_Click(object sender, EventArgs e)
{
    var grade = int.Parse(someTextBox.Text);
    Grade.Nested(grade);
}

I'm assuming the grade comes from a text box you have on the form, and I've skipped all validation and error handling.


Also, the Number property and field don't do anything in the code you have shown.

You might also find Code Review.SE a good place to get feedback on your coding style.