2017-02-08 105 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