public static int myPI = 3.14; 这样就可以在工程中的任何地方引用这个全局变量了 应用方法,类名.myPI
public class Global { public static int globalVariable; }// Access: int tmp_variable = Gloabal.globalVariable;
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;namespace morearg { /// <summary> /// Form1 的摘要说明。 /// </summary> /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public static int mywidth=600; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在任何处引用时只要用 Form1.mywidth就可以了。
c#有没有全局变量 , 你可以用STATIC类型来保存数据
c#和java都没有全局变量和全局函数,所有的变量和函数都属于某个类所有;
c#有没有全局变量 , 你可以用STATIC类型来保存数据!
static 静态变量,用这个可以
用static倒是好用,但两个实例会互相干扰,这个怎么办呢?
public class xxx { public static int intXXX=1; } 用的时候 XXX X=NEW XXX(); ....=X.intXXX .......
public string varName;
这样就可以在工程中的任何地方引用这个全局变量了
应用方法,类名.myPI
{
public static int globalVariable;
}// Access:
int tmp_variable = Gloabal.globalVariable;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace morearg
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
/// public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public static int mywidth=600;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在任何处引用时只要用 Form1.mywidth就可以了。
{
public static int intXXX=1;
}
用的时候
XXX X=NEW XXX();
....=X.intXXX
.......