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;

但我收到以下错误:

错误 2 无法将类型“字符串”隐式转换为“InMoTool.Entry”

我真的不知道如何转换它。有人可能会指出路吗?

4

1 回答 1

5

使用此代码

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

您正在尝试将 a 分配stringEntry. 这不可能。

我认为你需要的是这样的:

 _screenTag = tagTextBox.Text;    
 _screen.nodeDictionary["Tag "] = new Entry {
                                        type=_screenTag.GetType(), 
                                        value=_screenTag
                                      };
于 2013-05-22T15:28:37.953 回答