2013-05-22 102 views
0

我創建了一個字典,它接受我的結構的值並存儲它們。填寫本如下所示:修改字典中的結構值

_screenEntry.type = typeof(string); 
_screenEntry.value = tagTextBox.Text; 
_screen.nodeDictionary.Add("Tag ", _screenEntry); 

我的結構看起來像這樣以供參考:

public struct Entry 
{ 
    public Object value; 
    public Type type; 
} 

如何過,我現在想修改我的第一家專賣店該值。我只是試着重新調用nodeDictionary.Add,希望它會寫我以前的條目。但是,我收到一個錯誤,說我的字典已經有一個名爲「Tag」的鍵,這個鍵是自解釋的。

快速谷歌搜索使我發現,如果我要重寫我的初始值,我只需要調用這一行:

_screenTag = tagTextBox.Text;  
_screen.nodeDictionary["Tag "] = _screenTag; 

,但我得到了以下錯誤:

Error 2 Cannot implicitly convert type 'string' to 'InMoTool.Entry'

我不知道如何將其轉換。有人可能指出的方式?

+0

'_screen.nodeDictionary [ 「變量」] .value的= _screenTag;'?或者創建一個新的Entry,並完全覆蓋字典中的值。 –

回答

5

有了這個代碼

_screenTag = tagTextBox.Text; // <-- is a string 
_screen.nodeDictionary["Tag "] = _screenTag; 

您正嘗試將string分配給Entry。這不可能。

我認爲你需要的是這樣的:

_screenTag = tagTextBox.Text;  
_screen.nodeDictionary["Tag "] = new Entry { 
             type=_screenTag.GetType(), 
             value=_screenTag 
             };