嗨,我想在Visual Studio上編寫一個C#應用程序。我已經創建了一個數組,主要是我試圖訪問一個窗體上的點擊事件,但它告訴我在當前上下文中不存在數組'字符'。我試圖將數組傳遞給窗體,但我仍然有同樣的問題。任何幫助將非常感謝這裏是我的代碼。從主窗口訪問數組
namespace WindowsFormsApplication10
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool[][] characters = new bool[27][]; // my array characters
characters[1][0] = true;
Application.Run(new Form1());
}
}
}
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
int cs1 = 0,cs2=0;
public Form1()
{
InitializeComponent();
}
public void pictureBox1_Click(object sender, EventArgs e)
{
if (characters[1][0] == true) // trying to access member of characters
{ // array but characters does not
// exist in the current context
pictureBox28.Visible = false;
}
}
}
}
你有兩個選擇:1,使變量靜態字段'Program',或2.將數組作爲參數傳遞給'Form1'的構造函數。 –
這不是幾乎完全相同的問題嗎? http://stackoverflow.com/questions/13870651/accessing-an-instance-in-main-from-a-form –