Felice Massaro

Felice Massaro

  • NA
  • 17
  • 8.1k

How can I access a variable in a private void?

Apr 26 2013 8:02 AM
I have a windows application that sends and receives messages to/from a microprocessor using the serial port.

The application is working fine and does what is supposed to do. Now, I need to make some elaboration with the data I receive back from serial and I would like to access the variable "value" in SetText method. How can I access the content of that variable from another method or class? Thanks for helping.



delegate void SetTextCallback(string text);
    private void SetText(string text)
    {
        if (this.txtOutput.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.BeginInvoke(d, new object[] { text });


        }
        else
        {
            txtOutput.AppendText(text);
        }



        // capture messages from serial port
        if (txtOutput.Text.Length > 0)
        {
            MatchCollection mc = Regex.Matches(txtOutput.Text, @"(\+|-)?\d+");
            if (mc.Count > 0)
            {
                long value = long.Parse(mc[mc.Count - 1].Value);


                if (value > 1 && value < 1000)
                {
                    textBox2.Text = value.ToString();
                }
                else if (value < 2000 && value > 1000)
                {
                    value = value - 1000;
                    textBox3.Text = value.ToString();
                }

            }
        }
    }
    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {

        try
        {
            SetText(serialPort1.ReadExisting());
        }

        catch (Exception ex)
        {
            SetText(ex.ToString());
        }

    }

Answers (2)