mercredi 8 février 2017

vb.net clear multi-key dictionary

Vote count: 0

I am writing a program to populate a .txt template file with values by replacing placeholders. The values are coming from an excel document. I am reading a map file that contains, line by line 3 values; a subheader in excel, the element name and the placeholder. these values are being placed into 3 arrays and then combined into a multi-key dictionary.

I want to be able to run the program several times without having to close it, So i need to clear the dictionary after it is finished running so i can run it again. I have been unable to clear using .clear() and not sure why. It works when I clear it and then list the content in the same sub but not when I clear it in one sub and then try to use it in another. I am declaring the dictionary at the class level and using it in multiple subs/functions.

'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

This is how I have been populating the dictionary

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

I recently tried declaring it in a seperate Class but do not know how to clear/close the Class and then re-declare it.

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

Thanks!!

asked 30 secs ago

Let's block ads! (Why?)



vb.net clear multi-key dictionary

Aucun commentaire:

Enregistrer un commentaire