Home > front end >  C# Calling methods in methods
C# Calling methods in methods

Time:12-29

I have a code that will calculate something when button is clicked. The calculation formulas will vary, based on which radio button that is checked. Some of the calculation input will stay the same, independant of what radio buttons that are checked. So what I want to do, is to create a method that will be called upon in two different methods (case 1 and case 2), but I can't access the variables defined in calculationInput when called upon in the method for case 1 and 2, I get "the name does not exist in the current context". Can anyone help?

See below for a code example:

// Calculation input that will stay the same for each case
private void calculationInput(oject sender, EventArgs e)
{
    double a = 100;
    double b = 200;
    double c = 300;
}

//Case 1
private void calculationCaseOne(object sender, EventArgs e)
{
    calculationInput(new object(), new EventArgs());
    double case1 = a b;
    label.Text=case1.ToString();
}

//Case 2
private void calculationCaseTwo(object sender, EventArgs e)
{
    calculationInput(new object(), new EventArgs());
    double case2 = a c;
    label.Text=case2.ToString();
}

private void button1_Click(object sender, EventArgs e)
{
    if (radioButton1.Checked == true)
    {
        calculationCaseOne(new object(), new EventArgs());
    }
    if (radioButton2.Checked == true)
    {
        calculationCaseTwo(new object(), new EventArgs());
    }
}

CodePudding user response:

In the current implementation, the variables a, b, and c are local variables defined within the calculationInput method, so they are not accessible from outside of that method.

You will need to define them as fields of the class rather than local variables within a method.

public partial class Form1 : Form
{
    // Fields that will hold the calculation input
    private double a;
    private double b;
    private double c;

    // Method that initializes the calculation input
    private void calculationInput()
    {
        a = 100;
        b = 200;
        c = 300;
    }

    // Case 1
    private void calculationCaseOne()
    {
        calculationInput();
        double case1 = a   b;
        label.Text = case1.ToString();
    }

    // Case 2
    private void calculationCaseTwo()
    {
        calculationInput();
        double case2 = a   c;
        label.Text = case2.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (radioButton1.Checked == true)
        {
            calculationCaseOne();
        }
        if (radioButton2.Checked == true)
        {
            calculationCaseTwo();
        }
    }
}

CodePudding user response:

The calculationInput function is not needed, just define the variables

 /* private void calculationInput(oject sender, EventArgs e)
{
    double a = 100;
    double b = 200;
    double c = 300;
}*/

private double a = 100;
private double b = 200;
private double c = 300;

CodePudding user response:

You might be better off declaring those values outside the methods.

double a = 100;
double b = 200;
double c = 300;

private void calculationCaseOne(object sender, EventArgs e)
{
   /* a, b, and c can be accessed here */
}

private void calculationCaseTwo(object sender, EventArgs e)
{
    /* a, b, and c can be accessed here */
}

private void button1_Click(object sender, EventArgs e) { /* ... */ }

CodePudding user response:

Other answers are offering that you make these as private fields, but from what I can see in your button1_Click method you need these values to start at the values specified in calculationInput()

For this reason, you should be declaring these fields as either const in which case they are always that value and get inlined at compile time, or readonly in which case they may be initialized inline or at the constructor level (which is not necessary now, but in later refactoring opens up possibilities for the injection of the values you want on construction)

Const version, cannot be altered at any point in your code execution:

private const double _a = 100;
private const double _b = 200;
private const double _c = 300;

Readonly, cannot be altered post the ctor call:

//init here
private readonly double _a;
private readonly double _b;
private readonly double _c;

//*OR* you init them here, in which case you can perform logic on them
public My Form()
{
    _a = 100;
    _b = 200;
    _c = 300;

    /*
    //an example of injecting in values
    if (boolMeaningSomething)
    {
        //set to default -> 0.0
        _a = default(double);
        _b = default(double);
        _c = default(double);
    }    
    else
    {
        //set to your valuues
        _a = 100;
        _b = 200;
        _c = 300;
    }
    */
}
  • Related