vb.net清除多键词典

vb.net清除多键词典,vb.net,dictionary,Vb.net,Dictionary,我正在编写一个程序,通过替换占位符用值填充.txt模板文件。这些值来自excel文档。 我正在读取一个包含逐行3个值的映射文件;excel中的子标题、元素名称和占位符。这些值被放入3个数组中,然后合并到一个多键字典中 我希望能够在不关闭程序的情况下多次运行该程序,因此我需要在程序运行完成后清除字典,以便再次运行它。我无法使用.clear()清除,也不确定原因。 当我清除它,然后在同一个子目录中列出内容时,它会起作用,但当我在一个子目录中清除它,然后尝试在另一个子目录中使用它时,它不会起作用。 我

我正在编写一个程序,通过替换占位符用值填充.txt模板文件。这些值来自excel文档。 我正在读取一个包含逐行3个值的映射文件;excel中的子标题、元素名称和占位符。这些值被放入3个数组中,然后合并到一个多键字典中

我希望能够在不关闭程序的情况下多次运行该程序,因此我需要在程序运行完成后清除字典,以便再次运行它。我无法使用.clear()清除,也不确定原因。 当我清除它,然后在同一个子目录中列出内容时,它会起作用,但当我在一个子目录中清除它,然后尝试在另一个子目录中使用它时,它不会起作用。 我在类级别声明字典,并在多个子/函数中使用它

'Dictionary
Dim ObjDict As New Dictionary(Of ObjKey, String)

Structure ObjKey
    Public CIQ_Obj_Key As String
    Public CIQ_Sec_Key As String
    Public YAML_Tag_Key As String
End Structure
这就是我一直在填充字典的方式

Do Until xx = RowUpperBound

    If xlWorkSheet.Cells(xx, ObjHeaderColInt).Value = CIQ_Obj_Txt Then
        'MsgBox("Found " & CIQ_Obj_Txt & " in row " & xx)
        XlCell = CType(xlWorkSheet.Cells(xx, ValHeaderColInt), excel.Range)
        If IsNothing(XlCell.Value2) Then
            Write_Error_("No value found for " & CIQ_Obj_Txt & " at row " & xx)
        Else
            CellStr = XlCell.Value2.ToString.Trim
            TxtBxDebugBox.Text += "Found " & CIQ_Obj_Txt & ": " & CellStr & " at " & xx & vbNewLine
            GlobalVariables.ObjDict.Add(New GlobalVariables.ObjKey() With {.CIQ_Obj_Key = CIQ_Obj_Txt,
                                                                      .CIQ_Sec_Key = CIQ_Sec_Txt,
                                                                     .YAML_Tag_Key = YAML_Tag_Txt}, CellStr)
        End If
            Exit Do
    ElseIf xx = (RowUpperBound - 1) Then
            Write_Error_("Cannot find " & CIQ_Obj_Txt & " under " & CIQ_Sec_Txt)
            Exit Do
    End If
        xx += 1
Loop
我最近尝试在一个单独的类中声明它,但不知道如何清除/关闭该类,然后重新声明它

Public Class GlobalVariables
Public Shared ObjDict As New Dictionary(Of ObjKey, String)

Structure ObjKey
    Public CIQ_Obj_Key As String
    Public CIQ_Sec_Key As String
    Public YAML_Tag_Key As String
End Structure

End Class

谢谢

使用这样的类时,最好添加iDisposable支持

不要使用共享。。。。它使数据在所有实例上共享

Private GB as GlobalVatiables

Public Class GlobalVariables
    Implements IDisposable

    Public ObjDict As New Dictionary(Of ObjKey, String)

    Structure ObjKey
        Public CIQ_Obj_Key As String
        Public CIQ_Sec_Key As String
        Public YAML_Tag_Key As String
    End Structure

    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
                ObjDict.Clear()
                ObjDict = Nothing
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

End Class
完成后,只需调用dispose方法

GB.Dispose

你说当你尝试用另一种方法使用字典时,它不起作用。。。你说的不工作到底是什么意思?例外情况详情请。。。如果一切都失败了,objDict.DIspose()后跟objDict=newdictionary(ObkKey,String)应该可以完成任务…@MartinMilan,你会这么想的,不是吗。但是由于某些只有微软知道的原因。。。字典没有Dispose方法。您遇到了什么异常?发生了什么事?