静态类和窗体传值
静态类StaticData.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体传值 { public class StaticData { string _name; public string Name { get { return _name; } set { _name = value; } } int _age; public int Age { get { return _age; } set { _age = value; } } public void SayHi() { MessageBox.Show("Hi"); } public void Dispose() { } } }
一般类DynaData.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体传值 { public class Dynadata { string _name; public string Name { get { return _name; } set { _name = value; } } int age; public int Age { get { return age; } set { age = value; } } public void sayHi() { MessageBox.Show("Dynamic Hi"); } public void Dispose() { } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体传值 { static class Program { public static StaticData _staticData; /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { _staticData = new StaticData(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); Dispose(); } public static void Dispose() { _staticData.SayHi();//此时_data的值为Form1中两个userControl改变后的值 _staticData.Dispose(); } } }
Form1.cs
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 窗体传值 { public partial class Form1 : Form { public StaticData _staticData; // public Dynadata _dynaData = new Dynadata(); public Form1() { _dynaData.Name = "dyna cat "; _dynaData.Age = 1; InitializeComponent(); this.userControl11 = new UserControl1(ref _dynaData); this.userControl21 = new UserControl2(ref _dynaData); _staticData = Program._staticData; _staticData.Name = string.Empty; _staticData.Age = 0; } private void button1_Click(object sender, EventArgs e) { textBox1.Text = _staticData.Name + "\n"; textBox1.Text += _staticData.Age + "\n"; _staticData.SayHi(); } private void button2_Click(object sender, EventArgs e) { textBox2.Text = _dynaData.Name + "\n"; textBox2.Text += _dynaData.Age + "\n"; _dynaData.sayHi(); } } }
userControl1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace 窗体传值 { public partial class UserControl1 : UserControl { public StaticData _staticData; //此处需要初始化,否则button1_Click(object sender, EventArgs e)出错 public Dynadata _dynaData = new Dynadata(); public UserControl1(ref Dynadata dynaData) { InitializeComponent(); _staticData = Program._staticData; _dynaData = dynaData; } public UserControl1() { InitializeComponent(); _staticData = Program._staticData; } private void button1_Click(object sender, EventArgs e) { _staticData.Name = "static cat "; _dynaData.Name = "dynamic cat"; } } }
userControl2.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 窗体传值 { public partial class UserControl2 : UserControl { public StaticData _staticData; public Dynadata _dynaData = new Dynadata(); public UserControl2(ref Dynadata dynaData) { InitializeComponent(); _staticData = Program._staticData; _dynaData = dynaData; } public UserControl2() { InitializeComponent(); _staticData = Program._staticData; } private void button1_Click(object sender, EventArgs e) { _staticData.Age = 10; _dynaData.Age = 11; } } }
程序可以有效更改静态类成员的值,userControl中更改的值也会在Program中更新,但是对于动态类,则与预期不符合