靜態類和窗體傳值
阿新 • • 發佈:2017-12-05
rendering com img 分享圖片 aaa space 動態 pen .com
靜態類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; }View Codeset { _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 { returnView Code_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中更新,但是對於動態類,則與預期不符合
靜態類和窗體傳值