2017-02-08 53 views
0

我正在写一个程序,通过替换占位符来填充带有值的.txt模板文件。这些值来自excel文档。 我正在阅读一个包含逐行3值的地图文件; excel中的子标题,元素名称和占位符。这些值被放入3个数组中,然后组合成多键字典。vb.net清除多键词典

我希望能够多次运行该程序而不必关闭它,所以我需要在完成运行后清除字典,以便我可以再次运行它。我一直无法清除使用.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 

谢谢!!

+0

你说,当你尝试使用字典,从一个不同的方法,这是行不通的......究竟是什么意思做通过不起作用?异常细节请...失败的一切,objDict.DIspose()后面跟着objDict =新DIctionary(的ObkKey,字符串)应该做的工作... –

+0

@MartinMilan,你会认为你不会。但出于某种原因,只有Microsoft才知道......字典没有Dispose方法。 –

+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