vb.net清除多键字典

时间:2017-02-08 16:01:11

标签: vb.net dictionary

我正在编写一个程序,通过替换占位符来填充带有值的.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

我最近尝试在单独的Class中声明它,但不知道如何清除/关闭Class然后重新声明它。

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

谢谢!

1 个答案:

答案 0 :(得分:0)

使用类似的课程时,最好添加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