Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在字典中存储对象时出现问题_Vb.net_Dictionary - Fatal编程技术网

Vb.net 在字典中存储对象时出现问题

Vb.net 在字典中存储对象时出现问题,vb.net,dictionary,Vb.net,Dictionary,下面是我的代码的示例输出,这是一个简单的聊天程序: John says: Hello there! Marsha says: Hi there! John says: First sentence. Marsha says: Second Sentence. 在textbox控件中,它显示为如上所示。但是,在用于存储对话的字典中,它将如下所示: John says: First sentence. Marsha says: Hi there! John says: First senten

下面是我的代码的示例输出,这是一个简单的聊天程序:

John says: Hello there!
Marsha says: Hi there!
John says:  First sentence.
Marsha says:  Second Sentence.
在textbox控件中,它显示为如上所示。但是,在用于存储对话的字典中,它将如下所示:

John says: First sentence.
Marsha says: Hi there!
John says: First sentence.
Marsha says: Second sentence.
我已经检查了我的代码好几次了。。。就我个人而言,我不知道我在这件事上到底出了什么差错

我已将问题追溯到sendmsgbutton方法,如下所示:

Private Sub sendMsgButton_Click(sender As System.Object, e As System.EventArgs) Handles sendMsgButton.Click
    If rtnConnectStatus(t) = False Then
        RaiseEvent statusCheck("Not Connected" + ControlChars.CrLf)
    Else
        Dim completeMsg As String
        msg.Name = nameText.Text
        msg.Message = msgTxt.Text
        completeMsg = msg.ToString
        msgRecorded.Text &= completeMsg
        RaiseEvent statusCheck("Message Sent." + ControlChars.CrLf)
        msgList.Add(msgListIndex, msg)
        'RaiseEvent debugBox(msg, msgListIndex)
        msgListIndex += 1
        RaiseEvent DataSend(completeMsg)
        msgTxt.Clear()
    End If
End Sub
下面是msgList继承自的dictionary类:

Public Class MsgDictionary
     Inherits DictionaryBase

Public Property Item(ByVal key As Integer) As MsgObj
    Get
        Return CType(Dictionary(key), MsgObj)
    End Get
    Set(ByVal m As MsgObj)
        Dictionary(key) = m
    End Set
End Property

Public Sub Add(ByVal index As Integer, ByVal m As MsgObj)
    Dictionary.Add(index, m)
End Sub

End Class 
我的下一个测试是看它是否只是消息值,或者名称值是否也受此影响

提前感谢您在这方面提供的任何帮助/建议


编辑:只是澄清一下,每个字典条目的名称和字符串部分都是单个字典对象的属性。

问题很清楚:当现实需要不同的东西时,您正在考虑一个字典执行一个接一个的关联。您的代码将“John说:”关联到“Hello there!”,然后关联到“First句子”(您最终看到的值)。你要做的是将“约翰说:”与一系列行动联系起来

如果要依赖字典,应重新定义字典,使其能够将每个键与值列表相关联,即:

Dim newDic = New Dictionary(Of String, List(Of String))
然后你可以用正确的信息填充它。例如:

Dim msgList As New List(Of String)
msgList.Add("Hello there!")
msgList.Add("First sentence.")
newDic.Add("John says:", msgList)

从逻辑上讲,您必须根据实际需要调整此代码(即重新定义自定义词典和数据类型)。

首先,感谢您的回复。我编辑了最初的回复,但我也想和你核实一下。字典中的每个条目都是一个消息对象,每个对象中有两个变量(名称和消息)。根据你的回答,这看起来仍然适用,但我想确定。是的,一点问题都没有。如果定义了包含两个属性(名称和消息)的特定类型/类,则可以使用它创建一个列表,就像使用字符串一样;因此,您将有一个夫妻姓名信息列表。只需使用我提出的结构,将“string”替换为“MsgObj”。最后对如何解决这个问题发表评论,因为这个答案有助于找到额外的数据。(这主要是针对那些发现这个问题的人。我发现它仍在继续,即使是在将数据放入对象时。不过,我也有我的“MsgObj”对象作为全局变量。由于字典仍在引用该变量,因此它在字典中正在更改和复制自身。因此,我将其移动到send方法中的局部变量。在确保数据唯一性后,一切正常进行。再次感谢。