0

Can someone tell me how you can use a variable that has been defined within a textbox control in other controls and methods. In standard methods this is fine but not sure about controls. The alternative is to make it a global variable but I don't want to do that. The code is below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clean_Encoder_Box
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void goBtn_Click(object sender, EventArgs e)
    {

    }

    private void freqValueTb_TextChanged(object sender, EventArgs e)
    {
        string freqValueSt = freqValueTb.Text;
        ushort freqValueUs = ushort.Parse(freqValueSt);
    }
}
}
Kenny Barber
  • 245
  • 2
  • 11
  • 1
    What do you mean by "variable in a button"? and what do you want to do with it? – shahkalpesh Jan 20 '19 at 18:01
  • I have a variable called freqValueUs that I want to use elsewhere such as goBtn_Click method. I could just set it as global variable or set it within goBtn_Click. – Kenny Barber Jan 20 '19 at 18:16
  • There is nothing special with the method `freqValueTb_TextChanged`, it's just an ordinary function that happened to be linked to the `TextChanged` event. You'd use it as you use any other function. – Matan Shahar Jan 20 '19 at 18:32
  • Why don't you simply use the value of freqValueTb when you need it? What is the motive to do this code in the TextChanged event? – Steve Jan 20 '19 at 18:33

1 Answers1

1

Variables declared in a method, i.e. locally, are only visible within this method. Declare fields on class level instead.

public partial class Form1 : Form
{
    private string _freqValueSt;
    private ushort _freqValueUs;

    public Form1()
    {
        InitializeComponent();
    }

    private void goBtn_Click(object sender, EventArgs e)
    {
        // _freqValueSt and _freqValueUs are visible here.
    }

    private void freqValueTb_TextChanged(object sender, EventArgs e)
    {
        _freqValueSt = freqValueTb.Text;
        _freqValueUs = ushort.Parse(freqValueSt);
    }
}

or create a property instead

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

    private void goBtn_Click(object sender, EventArgs e)
    {
        ushort f = Frequency;
    }

    private ushort Frequency => ushort.Parse(freqValueTb.Text);
}

Or use a safe variant

private ushort? Frequency {
    get {
        if (ushort.TryParse(freqValueTb.Text, out ushort f)) {
            return f;
        }
        return null;
    }
}

See also: - Fields (C# Programming Guide)

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Hi Olivier, I read alot about keeping variables local to a function in C. Does this apply to C# or is it fine to just use class level fields? – Kenny Barber Jan 23 '19 at 05:55
  • 1
    Use only class level fields for storing values whose life-time exceeds the life-time of a method call. Otherwise use local variables. Instead of parsing the frequency in the textbox, you could create a property returning the requested value. In other cases you can use method parameters to pass values to another method. – Olivier Jacot-Descombes Jan 23 '19 at 13:26