我如何访问这个方法中的变量
本文关键字:方法 变量 何访问 访问 | 更新日期: 2023-09-27 18:11:04
我有一个windows应用程序,它使用串行端口向微处理器发送和接收消息。
应用程序工作正常,并做了应该做的事情。现在,我需要对从串行接收回的数据进行一些详细说明,并且我想访问SetText方法中的变量"value"。如何从另一个方法或类访问该变量的内容?谢谢你的帮助。
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());
}
}
考虑一下:
创建属性
public long Value { get; set; }
在你的代码中使用它。
if (txtOutput.Text.Length > 0)
{
MatchCollection mc = Regex.Matches(txtOutput.Text, @"('+|-)?'d+");
if (mc.Count > 0)
{
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();
}
}
如果您想确保此属性保留其值,则使用静态属性
public static long Value { get; set; }
如果数据将在多个地方使用,那么不要犹豫,只需创建一个包含将在方法之间共享的输出变量列表的类。为该类中的变量创建属性。现在为这个类全局创建一个对象,并将从微处理器检索到的值分配给这个全局声明对象中的属性。你可以在任何地方访问它。因为这是一个windows应用程序,数据将保留,直到您清除或关闭应用程序。
下面是代码。我有一个文本框和两个按钮在windows应用程序。一个按钮来获取数据和另一个显示数据。数据是使用文本框从用户处获取的。在获得数据后,点击显示数据按钮,它将显示推送到对象的数据,你想要多少次就显示多少次。
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
// Declare Global Variable
DataHolder objDataHolder = new DataHolder();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Here use your code to load the data retrieved from Microprocessor
objDataHolder.UserData = txtUserData.Text;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(objDataHolder.UserData);
}
}
// Class to hold Data
public class DataHolder
{
// Here have a list variables that you need to maintain that are retrieved from Microrocessor.
private string _userdata = string.Empty;
// Here have a list Properties that you need to maintain that are retrieved from Microrocessor.
public string UserData
{
get
{
return _userdata;
}
set
{
_userdata = value;
}
}
}
}
您可以使用"Static"变量或实例变量访问其他类中的变量
public class Demo1
{
//Static variable can be accessed without instantiating an instance of Demo1
public static int Number; //Demo1.Number
public string Info {get;set;}
}
public class AnotherClass
{
void DoSth()
{
Demo1.Number ++;
}
}
或者如果你有一个Demo1的实例,输入demo1Instance
demo1Instance.Info="Sth you like";
这就是我所做的,现在它正在工作。谢谢大家的好建议。我很确定,我很快就会在应用程序的附加开发中使用您的示例。
internal long value;
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)
{
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();
}
}
}
}