Dictionary<string, string> propertyCompany = new Dictionary<string, string>();//gloabal variable
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack){
propertyCompany .add("a","1");
propertyCompany .add("b","2");
propertyCompany .add("c","3");
}
}
protected void btnGetProperty_Click(object sender, EventArgs e)
{
string a=propertyCompany["a"];//error this key is not exist
//propertyCompany is null
}
定義propertyCompany
並填入form_load
。在點擊按鈕propertyCompany
是空的!?全局變量爲空
我使用static
但我不明白有時候說錯誤爲空。
'null'或只是空的?你真的認爲你在'Page_Load'中添加的三個項目不存在於'btnGetProperty_Click'中嗎? –
@Graham除非有代碼,我們沒有看到它影響的東西,我敢肯定它是「沒有數據」而不是'null'。初始化器將被添加到構造函數的開始處,並且將在每個頁面加載時運行。 – dlev
@dlev:是的,我確定是這種情況,我只是試圖提示OP更仔細地檢查一些事情...... –