0

I am getting a

'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

when i run my code, everthing seems to be fine, i have been unable to find the bug. Here is path of my class caling my get/set

   foreach (Tran transaction in transactions)
        {
            Account sAcc = items[transaction.ID1];//getting error here
            Account dAcc = items[transaction.ID2];
            decimal tempResult = sAcc.Money - transaction.Amount;
            foreach (char s in sAcc.BorP)
                Console.WriteLine(s);

And here is my get/set class

 public class Tran
{
    public int ID1 { get; set; }
    public int ID2 { get; set; }
    public decimal Amount { get; set; }
}

It was running before and i ran some more test and i keep getting this error and dont know what could be causing int. Thanks for you help

2 Answers2

0

You can use the TryGetValue method of the dictionary.

Here is a sample code of using

object result; // Un-Initialized instance result
myCollection.TryGetValue("Test", out result); // If the key exists the method will out the value

Grabbed from another SO post.

From MSDN's entry on Dictionary.TryGetValue Method:

This method combines the functionality of the ContainsKey method and the Item property.

If the key is not found, then the value parameter gets the appropriate default value for the value type TValue; for example, 0 (zero) for integer types, false for Boolean types, and null for reference types.

Use the TryGetValue method if your code frequently attempts to access keys that are not in the dictionary. Using this method is more efficient than catching the KeyNotFoundException thrown by the Item property.

This method approaches an O(1) operation.

Also reference in this post to lookup the performance of Indexer vs TryGetValue

What is more efficient: Dictionary TryGetValue or ContainsKey+Item?

Community
  • 1
  • 1
DevEstacion
  • 1,947
  • 1
  • 14
  • 28
-2

The issue you are having is that the items you are trying to read from the items array don't exist.

Use either

if (items.ContainsKey(transaction.ID1) && items.ContainsKey(transaction.ID2))
{
  // Your code.
} else {
  // Signal an error.
}

Or if you have a default account class that you want to use when the transactions do not exist you could use something like this.

if (!items.TryGetValue(transaction.ID1, out sAcc))
  items.Add(transaction.ID1, sAcc = new Account);
if (!items.TryGetValue(transaction.ID2, out dAcc))
  items.Add(transaction.ID2, dAcc = new Account);
// Your code.

Otherwise if the transaction ID:s should always be in item the problem with your code is likely outside of the code snippets you shared here. I would check what the ID:s are you are trying to look up and do a sanity check.

  • Your code is invalid for multiple reasons: **a)** `TryGetKey` only exists for `ImmutableSortedDictionary` which the OP is almost certainly not using **b)** the syntax `items.Add(transaction.ID1, sAcc = new Account)` is invalid C# syntax on multiple levels. (`=` syntax in a method invocation does not exist and `new Account` is invalid without either `()` or `{}`) **c)** The OP almost certainly wants the value -- ergo `TryGetValue`. – Kirk Woll Dec 16 '14 at 02:56
  • Why don't just use TyGetValue? – DevEstacion Dec 16 '14 at 03:03
  • Sorry, I did mess up the TryGetKey vs TryGetValue. The assignments work fine though as written. I use this pattern all the time. – HenrikJohnson Dec 16 '14 at 03:25