Posted: 7/14/2011
I am using a Dictionary of(String, Dictionary(Of String, String()()) to store some data that I am pulling out of a flat database file. I am having issues with Key Not Found Exceptions. Without using a Try Catch block, how can I protect against attempting to use keys that are not in the dictionary?
Here is a snippet of my code: This Function creates strings of xml data sheets which can be inserted into Excel .xlsx files. The line in question is highlighted with comments.
Public Function dictionaryToXMLSheet(ByVal rowsCols As Integer, ByRef dictionary _ As Dictionary(Of String, Dictionary(Of String, String()()))) As String Dim dictXMLSheets As New Dictionary(Of String, String) Dim xmlString As String Dim tempRow As Integer For Each kvp As KeyValuePair(Of String, Dictionary(Of String, String()())) In dictionary dictXMLSheets.Add(kvp.Key, "") xmlString = "" 'Portions of the code emitted 'How can I check the existence of kvp.Value("HCI") before I attempt to use it??? xmlString &= writeXMLDataCell(kvp.Value("HCI"), tempRow, "F" & CStr(tempRow), False, False) Next Dim xlsxFile As String = Me.cFileName.Split(".")(0) & "_XLSX.xlsx" Dim xlsx2 As New XLSXWorkbook(xlsxFile, dictXMLSheets.Keys.ToList, dictXMLSheets.Values.ToList) Me.boolCreated = True Return xlsxFile End Function
Public Function dictionaryToXMLSheet(ByVal rowsCols As Integer, ByRef dictionary _
As Dictionary(Of String, Dictionary(Of String, String()()))) As String Dim dictXMLSheets As New Dictionary(Of String, String) Dim xmlString As String Dim tempRow As Integer For Each kvp As KeyValuePair(Of String, Dictionary(Of String, String()())) In dictionary dictXMLSheets.Add(kvp.Key, "") xmlString = "" 'Portions of the code emitted 'How can I check the existence of kvp.Value("HCI") before I attempt to use it??? xmlString &= writeXMLDataCell(kvp.Value("HCI"), tempRow, "F" & CStr(tempRow), False, False) Next Dim xlsxFile As String = Me.cFileName.Split(".")(0) & "_XLSX.xlsx" Dim xlsx2 As New XLSXWorkbook(xlsxFile, dictXMLSheets.Keys.ToList, dictXMLSheets.Values.ToList) Me.boolCreated = True Return xlsxFile End Function
Posted: 7/19/2011
Best to use TryGetValue() for the dictionary object. So first to check the key through the function then you can use kvp.Value("HCI") . Not so sure that the code will work but think it may help you. Write the following code
If dictXMLSheets.TryGetValue("HCI", value) Then xmlString &= writeXMLDataCell(kvp.Value("HCI"), tempRow, "F" & CStr(tempRow), False, False) End If
instead of
xmlString &= writeXMLDataCell(kvp.Value("HCI"), tempRow, "F" & CStr(tempRow), False, False)
If you want more about the function use the following links:
http://msdn.microsoft.com/en-us/library/bb347013.aspx#Y1172
Subhankar, thanks for your reply but I am still having problems. Here is what I have in my code. This is always evaluating to true and I am still encountering KeyNotFoundExceptions.
Dim value As Dictionary(Of String, String()()) = Nothing If dictionary.TryGetValue(kvp.Key, value) Then xmlString &= writeXMLDataCell(kvp.Value("HCI"), tempRow, "I" & CStr(tempRow), False, True) End If
Here is the declaration for 'dictionary' and for 'kvp'
ByVal dictionary As Dictionary(Of String, Dictionary(Of String, String()())) For Each kvp As KeyValuePair(Of String, Dictionary(Of String, String()())) In dictionary
The TryGetValue is set up as such:
TryGetValue(key as String, ByRef value as System.Collections.Generic.Dictionary(Of String, String()())) as Boolean
I know that the key is going to be kvp.key. However, I do not know whether certain values, in this case kvp.Value("HCI"), will exist because the input files are always different. Thus, I need to prevent any errors by protecting the code with some sort of if statement that checks for the existence of the value. The problem is that I don't know what the if statement should look like, I have never worked with Dictionaries, and this is code that I inherited.
Please Help.
Posted: 7/22/2011
Well,
very sorry that my thought hadn’t help u so much, but I think as u have coded dictXMLSheets.Add(kvp.Key, ""); and whether u calling the function the following manner
dictionary.TryGetValue(kvp.Key, value)
it must always be true, coz here u should have to define a key physically not a dynamic key from a collection. I m also not so much experience in Dictionaries, but trying to help u. I think you may follow the link below where lot’s of example have shown on this topic………..
http://www.dotnetperls.com/~dictionary
Posted: 8/3/2011
I ended up just using a Try-Catch statement in my code which is probably not a good coding practice for this situation. It is kind of like letting a car drive off a cliff and catching it with a net. Ideally, one would rather put up a gaurd rail so the car cannot drive off.
Here is the code:
For Each kvp As KeyValuePair(Of String, Dictionary(Of String, String()())) In dictionary Try xmlString &= writeXMLDataCell(kvp.Value("HCI"), tempRow, "I" & CStr(tempRow), False, True) Catch ex As Exception xmlString &= getMissingValue("HCI") End Try ................... Next
Thank you for your inputs.