Jon C
Jon C

Reputation: 1

c# Should I use a output in my void method?

 private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            int Number = Convert.ToInt32(textBox1.Text);
            HalfNumber(Number);
            textBox1.Focus();
            textBox1.SelectAll();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    private void HalfNumber(int numberToUse)
    {
        double x = numberToUse / 2.0;
        MessageBox.Show("Half of the number is " + x.ToString());
    }
}
}

The code above is an example.
I thought my instructor said never to use a output in a method because in case of testing code, the code might be on the other side of the world and unable to see whats going on.
I may have misunderstood her, but I would like someone to explain

Upvotes: 0

Views: 119

Answers (2)

Murat Oğuzhan
Murat Oğuzhan

Reputation: 885

void means you are can not return any value. If you want to output you are need to specify it. After that, you can use return values.

For example:

private double HalfNumber(int numberToUse)
{
    double x = numberToUse / 2.0;
    return x;
}

And you can use like that

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        int Number = Convert.ToInt32(textBox1.Text);
        double result = HalfNumber(Number);

        MessageBox.Show("Half of the number is " + result.ToString());
        textBox1.Focus();
        textBox1.SelectAll();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private double HalfNumber(int numberToUse)
{
    double x = numberToUse / 2.0;
    return x;
}

Upvotes: 1

TheGeneral
TheGeneral

Reputation: 81493

Its hard to know exactly what she meant. However putting a dialog in a function that does something discrete is unexpected.

It would be more logical to return a number from the HalfNumber Method, and then show the dialog containing the results

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        int Number = Convert.ToInt32(textBox1.Text);
        double myHalfNumber = HalfNumber(Number);

        MessageBox.Show("Half of the number is " +myHalfNumber.ToString());

        textBox1.Focus();
        textBox1.SelectAll();

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

private double HalfNumber(int numberToUse)
{      
    return numberToUse / 2.0;
}

Separation of Concerns

In software engineering, Separation of Concerns refers to the delineation and correlation of software elements to achieve order within a system. Through proper separation of concerns, complexity becomes manageable

don't repeat yourself (DRY)

A principle of software development aimed at reducing repetition of software patterns, replacing them with abstractions; and several copies of the same data, using data normalization to avoid redundancy.

Putting a HalfNumber into its own discrete method allows you to reuses your code, and separates that logical out in to is only maintainable and predictable logic

Upvotes: 3

Related Questions