http://www.cnblogs.com/agian/articles/1542317.html C#中不再有全域變數、函數或者常量。所有東西都封裝在類中,包括執行個體成員(通過類的執行個體---對象可以訪問)和靜態成員(通過資料類型)。
這樣使C#代碼更加易讀且有助於減少潛在的命名衝突。
1 . 可以用靜態全域:public static int myPI = 3.14; 這樣就可以在工程中的任何地方引用這個全域變數了 應用方法,類名.myPI
2. public class xxx
{
public static int intXXX=1;
}
用的時候
XXX X=NEW XXX();
....=X.intXXX
.......
//全域變數定義方式之一
public static string bbb; //最簡單的定義全域變數的方法
//定義全域變數方式之二
public class VarTrans //用靜態變數(必須使用靜態變數,必須!!!)作全域變數,看看與上面的區別,上面的方法更靈活多變
{
public static string ss; //靜態變數用 “類名.變數名“ 的方式訪問,注意中間的點。不用採用對類進行執行個體引用的方式訪問
}
//全域變數定義方式之三
public class MyVar //定義全域變數類。 可以賦值,但是為什麼只能在賦值後讀取,如何能直接讀取???????
{
private string string1;
private string string2;
private int i1;
public string String1
{
get
{
return string1;
}
set
{
string1 = value;
}
}
public string String2
{
get
{
return string2;
}
set
{
string2 = value;
}
}
public int I1
{
get
{
return i1;
}
set
{
i1 = value;
}
}
}
//以上三種定義全域變數的方法究竟都有哪些優缺點,在什麼情況下使用,代碼是否存在問題??? 需要探究!!!
//定義全域常量
public class ConsTrans
{
public const string aa = "I am Constant Var";
}
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 96);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(272, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 128);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(240, 23);
this.button1.TabIndex = 1;
this.button1.Text = "顯示全域變數儲存的值";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(40, 224);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(240, 23);
this.button2.TabIndex = 2;
this.button2.Text = "改變全域變數的值";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(208, 256);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panel1.Controls.Add(this.label1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(320, 56);
this.panel1.TabIndex = 4;
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "C# 全域變數實驗";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.TabIndex = 1;
this.label2.Text = "全域變數的值:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 192);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(272, 21);
this.textBox2.TabIndex = 5;
this.textBox2.Text = "textBox2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 168);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 6;
this.label3.Text = "新值:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 285);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Name = "Form1";
this.Text = "VarValueTrans";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//MyVarClass d2 = new MyVarClass();
//d2.MyString = "bbb";
//this.textBox1.Text = d2.MyString.ToString();
this.textBox1.Text = VarTrans.ss.ToString();
MessageBox.Show(this,bbb);
bbb = "changed";
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.textBox1.Clear();
this.textBox2.Clear();
MyVar g_var = new MyVar();
g_var.String1 = "aaa";
g_var.String2 = "bbb";
g_var.I1 = 100;
MessageBox.Show(this,g_var.String1+" "+g_var.String2+" "+g_var.I1.ToString()+" "+ConsTrans.aa);
VarTrans.ss = "Static Var in Class 初始值";
bbb = "single static var";
}
private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show(this,bbb);
VarTrans.ss = this.textBox2.Text.ToString();
}
private void button3_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}