-1

I am new to c# and I know the answer is very simple I just could not find it through searching

I created two buttons the first one generates random values and the second one is an IF statement inside another button, but I`m getting a red line under 1value1 saying

the name does not exists in current context

private void button3_Click(object sender, EventArgs e)
{
    Random b = new Random();
    float value = b.Next(50, 100);
}
private void button2_Click(object sender, EventArgs e)
{
    if (value < MinValue)
    {
       textBox18.Text = ("warning");
       textBox18.ForeColor = Color.White;
       textBox18.BackColor = Color.Red;
    }
 }
3
  • 2
    float value has to be declared outside your button event handlers
    – croxy
    Commented Oct 19, 2016 at 11:42
  • please have a look here to understand scopes. Commented Oct 19, 2016 at 11:43
  • define globally value and MinValue Commented Oct 19, 2016 at 11:45

2 Answers 2

1

value is defined in the scope of the button3_Click and thus not accessible for the button2_Click. Put it as a variable of the class:

private int _minValue = 50;
private int _maxValue = 100;

private float _value = _maxValue;

private void button3_Click(object sender, EventArgs e)
{
    Random b = new Random();
    _value = b.Next(_minValue, _maxValue);
}

private void button2_Click(object sender, EventArgs e)
{
    if (_value < _minValue)
    {
        textBox18.Text = ("warning");
        textBox18.ForeColor = Color.White;
        textBox18.BackColor = Color.Red;
    }
}
1
  • @Lupin - If this solved the question please mark as solved :) Commented Oct 19, 2016 at 11:57
0

Set property GenerateMember=True

1
  • 1
    Can you expand this answer? Commented Apr 20, 2020 at 2:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.