0
votes

I am fairly new to VB net and have been playing around with dictionaries for the past week. I have a problem however when trying to do something rather complex with my dictionary look-up.

First, I should point out that I am filling my dictionary with a class object in order to store multiple values:

Class NodeLoad
    Public Property NodeName As String
    Public Property NodeCase As String
    Public Property NodeAxis As String
    Public Property NodeDir As String
    Public Property NodeValue As Double
End Class

And my problem lies in doing a dictionary look-up where my only option is to do a try catch for when the value I am looking for doesn't exist:

Try
   tempnodeitem = (From load In load_dict.Values Where load.NodeName = nodenum And load.NodeCase = pattern And load.NodeDir = dirarray(d)).First
   loadforce(d) = tempnodeitem.NodeValue
   Catch ex As Exception
   loadforce(d) = "0"
End Try

The above code runs, but it takes much longer than I would expect, and after a little research found that try/catch takes much longer than TryGetValue. The thing I would like to do (since it is a much for efficient function) is to use TryGetValue. However, as far as I know, it only works for one key and one value (TKey, TValue).

Can anyone give me an example of how to use TryGetValue with multiple conditions?

Or perhaps how to catch false dict look-ups without being resource intensive?

I am thinking a good way to approach this problem is using nested TryGetValue statements... or possibly multiple dicts or lists which can handle this problem differently.

I appreciate any input!

Thanks!

1
Don't use First(), use FirstOrDefault(). Default is Nothing. - Hans Passant
You can also test if tempnodeitem is nothing before attempting to read a property from an instance of an object the does not exist. - Steve

1 Answers

0
votes

As you're using a function anyway, I would tend to use function syntax in this case rather than query syntax. Is it possible that there could be more than one match to your conditions? There are four similar methods, i.e. First, Single, FirstOrDefault and SingleOrDefault, and there is never a case where more than one is appropriate. The choice of which to use comes down to two simple questions:

  1. Will there always be at least one match? If not then use one that ends with "OrDefault".
  2. Will there ever be more than one match? If not then use one that starts with "Single".

The answers to those two questions will always tell you which of the four methods to call.

Now, you're using a Dictionary in this case, right? What are the keys? I would have thought NodeName would be but I guess not. Anyway, assuming that there will be zero or one matches to your conditions, you would use SingleOrDefault. The code for FirstOrDefault would look exactly the same anyway:

Dim item = myDictionary.Values.SingleOrDefault(Function(nl) nl.NodeName = nodenum AndAlso
                                                            nl.NodeCase = pattern AndAlso
                                                            nl.NodeDir = dirarray(d))

loadforce(d) = If(item Is Nothing, 0.0, item.NodeValue)

Notice two other corrections to your code: the proper use of AndAlso instead of And as well as the assignment of a Double value to loadforce(d) rather than a String if there is no match. The NodeValue property is type Double so how can you want a Double if there is a match and a String if there isn't?