大 哥

导航

C# 全局变量

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();
  }
 }
}

posted on 2009-08-09 15:45  VIP-爷  阅读(65047)  评论(0编辑  收藏  举报