Resolved I really want to use a method in my program

357mag

Well-known member
Joined
Mar 31, 2023
Messages
58
Programming Experience
3-5
I have this program that inputs 3 integers and then determines which one is the largest. My program works fine, but I really want to write a method that contains the if statements and figures out which
number is the largest.

Problem is I don't know where to put the method header and the resulting code. Right now I've got it I think in the wrong spot cause I'm getting red squiggly lines.
C#:
namespace Find_Maximum_Value
{
    public partial class FormFindMaximumValue : Form
    {
        public FormFindMaximumValue()
        {
            InitializeComponent();
        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            int a, b, c, maximumValue;

            a = Convert.ToInt32(textBoxFirstInteger.Text);
            b = Convert.ToInt32(textBoxSecondInteger.Text);
            c = Convert.ToInt32(textBoxThirdInteger.Text);

            maximumValue = a;

            if (b > maximumValue)
                maximumValue = b;

            if (c > maximumValue)
                maximumValue = c;

            labelAnswer.Text = "Maximum value is " + maximumValue;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxFirstInteger.Text = " ";
            textBoxSecondInteger.Text = " ";
            textBoxThirdInteger.Text = " ";
            labelAnswer.Text = " ";
        }

        public static int Maximum(a, b, c)
        {
            int maximumValue = a;

            if (b > maximumValue)
                maximumValue = b;

            if (c > maximumValue)
                maximumValue = c;

        }
 
I got it going now. Had to experiment. You put the method header and all the code right underneath the closing brace of the calculate click header.
 
I got it going now. Had to experiment. You put the method header and all the code right underneath the closing brace of the calculate click header.
It doesn't matter where you put it. It's a member of that class so it needs to be inside the class and it obviously needs to be outside any other method but the order of the methods in the class is irrelevant. You could put it immediately before the Click event handler or even at the very top of the class and it would still work exactly the same way. It's good to have some organisational rules that you follow to keep your code orderly but, as far as the compiler is concerned, all that matters is that a member is in the class.
 

Latest posts

Back
Top Bottom