C#中如何定义全局变量及在各窗体中使用全局变量

时间:2021-04-16

  C#中如何定义全局变量及在各窗体中使用全局变量。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace NewTest
{
public class PublicValue
{
public static string FileName;
public static int[] aa;
}
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{

public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void MainFormLoad(object sender, EventArgs e)
{
this.Width=Screen.PrimaryScreen.WorkingArea.Width;
this.Height =Screen.PrimaryScreen.WorkingArea.Height;
this.Left =Screen.PrimaryScreen.WorkingArea.Left;
this.Top =Screen.PrimaryScreen.WorkingArea.Top;
}

void Button1Click(object sender, EventArgs e)
{
PublicValue.aa=new int[10];
PublicValue.aa[0]=10;
PublicValue.FileName="dfjlkjkldfsjl";
}
}
}

  以上定义的publicValue类,其中的变量可以在同一个命名空间中使用,包括在子窗体或非子窗口中调用。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章