你好,我正在尝试获取和设置从一个类到另一个类的调用和存储变量。
这是一个具有 2 个按钮和 2 个文本框的窗口窗体。
类实用程序是执行所有后端计算并存储变量ActiveUserId和ActiveUserName。现在我只想存储两个文本框的值,然后能够获取它们。
namespace WindowsFormsApplication1
{
class Utility
{
public int ActiveUserID;
public string ActiveUserName;
public int _ActiveUserID
{
get { return ActiveUserID;}
set { ActiveUserID = value; }
}
public string _ActiveUserName
{
get { return ActiveUserName; }
set { ActiveUserName = value; }
}
public void StoreActiveUser(int currentUserID, string currentUserName)
{
ActiveUserID = currentUserID;
ActiveUserName = currentUserName;
}
public int GetActiveUser()
{
return ActiveUserID;
}
}
}
另一类是
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Utility tool1 = new Utility();
tool1.StoreActiveUser(Int32.Parse(textBox1.Text), textBox2.Text);
MessageBox.Show(String.Format("the values are :{0} {1}", tool1.ActiveUserID.ToString(), tool1._ActiveUserName));
}
private void button2_Click(object sender, EventArgs e)
{
Utility tool1 = new Utility();
MessageBox.Show(String.Format("the values are :{0} {1}",tool1.ActiveUserID.ToString(),tool1._ActiveUserName));
MessageBox.Show(String.Format(tool1.GetActiveUser().ToString()));
}
}
}
我在文本框中输入一些值,然后按按钮1,消息正确显示值。
当我按下按钮 2 时,我应该从消息框中显示的实用程序类中获取活动用户 ID 和活动用户名的值,我得到的只是"0"
然后我添加了一个返回活动用户 ID 但返回 0 的函数;
我不是在button1_Click中永久存储活动用户ID的值吗?感谢您的任何帮助。
不是你的问题的答案,但你的实用程序类也可以改进。这些字段应该是私有的,并以下划线开头(按照惯例),属性应该是公共的:
class Utility
{
private int _activeUserID;
private string _activeUserName;
public int ActiveUserID
{
get { return _activeUserID;}
set { _activeUserID = value; }
}
public string ActiveUserName
{
get { return _activeUserName; }
set { _activeUserName = value; }
}
// You don't need this method, you can store id and username like this:
// tool1.ActiveUserID = currentUserID;
// tool1.ActiveUserName = currentUserName;
public void StoreActiveUser(int currentUserID, string currentUserName)
{
ActiveUserID = currentUserID;
ActiveUserName = currentUserName;
}
// no need for this method either, you can use this:
// int id = tool1.ActiveUserID;
public int GetActiveUser()
{
return ActiveUserID;
}
}
下一步可能是将属性转换为自动属性:
class Utility
{
public int ActiveUserID { get; set; }
public string ActiveUserName { get; set; }
}
您要在第二个按钮中创建一个新对象,请尝试以下操作:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Utility tool1;
public Form1()
{
InitializeComponent();
tool1 = new Utility();
}
private void button1_Click(object sender, EventArgs e)
{
tool1.StoreActiveUser(Int32.Parse(textBox1.Text), textBox2.Text);
MessageBox.Show(String.Format("the values are :{0} {1}", tool1.ActiveUserID.ToString(), tool1._ActiveUserName));
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(String.Format("the values are :{0} {1}", tool1.ActiveUserID.ToString(), tool1._ActiveUserName));
MessageBox.Show(String.Format(tool1.GetActiveUser().ToString()));
}
}
}
它们可能具有相同的名称 (tool1),但它们是 Utility 类的两个完全独立的实例。因此,存储在 button1_Click 中的 tool1 实例中的数据与 button2_Click 中的 tool1 实例中的数据不同。
您需要做的是更改 tool1 的范围,使其全局化为button1_Click和button2_Click。所以像这样:
Utility tool1 = new Utility();
private void button1_Click(object sender, EventArgs e)
{
tool1.StoreActiveUser(Int32.Parse(textBox1.Text), textBox2.Text);
MessageBox.Show(String.Format("the values are :{0} {1}", tool1.ActiveUserID.ToString(), tool1._ActiveUserName));
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(String.Format("the values are :{0} {1}",tool1.ActiveUserID.ToString(),tool1._ActiveUserName));
MessageBox.Show(String.Format(tool1.GetActiveUser().ToString()));
}
每次都会创建一个新的Utility
对象。您应该:
在
Form()
构造函数中创建单个Utility
对象,并将其另存为字段:private Utility Tool { get; set; } public Form() { InitializeComponent(); Tool = new Utility(); } private void button1_Click(object sender, EventArgs e) { Tool.StoreActiveUser(Int32.Parse(textBox1.Text), textBox2.Text); // ...
使
Utility
成为静态类,并引用Utility.StoreActiveUser
,而不是创建任何实例。
Utility tool1 = new Utility();
您在这里的问题是范围。每次调用该行代码时,您都在创建一个新的局部变量。您需要将单个实例实例化为类成员。