1

I have a few dictionary objects being combed through to get a value but all of them return null even though when I check to see if they contain the key the one that has the key returns true. I can also use a foreach to go through each one to get the value based on that key....I'm so lost

foreach (var item in treeView.SelectedItems)
{
    string reportName = item.Header.ToString();
    string reportPath = "";

    reportsAvail.TryGetValue(reportName, out reportPath);
    reports.TryGetValue(reportName, out reportPath);
    additionalReports.TryGetValue(reportName, out reportPath);

    bool test; 

    test = reportsAvail.ContainsKey(reportName);
    test = reports.ContainsKey(reportName);
    test = additionalReports.ContainsKey(reportName);

    foreach (var y in reportsAvail)
    {
        if (y.Key.ToString() == reportName)
        {
            textBlock1.Text = y.Value;
            reportPath = y.Value;
        }
    }
}

What's weird is it USED to work...I'm not sure what's stopping it

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Brian Crawford
  • 123
  • 1
  • 10
  • Did you assign `null` as the value of that key in the dictionary? – Servy Mar 24 '15 at 14:43
  • what is reports ? type ?... tru reports.Items[key] that should return if it exists.. – Muds Mar 24 '15 at 14:44
  • Your `foreach` is only going through `reportsAvail`, though in your debugging lines you're checking two other dictionaries. Are you sure that's the dictionary it's in? – Adam V Mar 24 '15 at 14:45
  • Check the case, it is case sensitive. put trim on key as well. – Hemant Malpote Mar 24 '15 at 14:46
  • its a dictionary @Servy When I check the dictionary during debugging it shows they key I'm looking for is there. The dictionary has no null keys or values – Brian Crawford Mar 24 '15 at 14:46
  • You are using `TryGetValue` three times and you're overwriting `reportPath` each time. So even if the first or second contained the `reportName`, if the third didn't contain it `reportPath` will be `null` again. – Tim Schmelter Mar 24 '15 at 14:46
  • @AdamV you are correct, I didn't make the other two right now because I was testing with something in reportsAvail before posting. I had done the other 2 before and they worked fine. – Brian Crawford Mar 24 '15 at 14:48
  • @TimSchmelter I bet that's it. I didn't even think of that. I will shove it in a switch and see if that goes anywhere! – Brian Crawford Mar 24 '15 at 14:49

1 Answers1

5

You are using TryGetValue three times and you're overwriting reportPath each time. So even if the first or second contained the reportName, if the third didn't contain it reportPath will be null again.

Maybe this fixes it:

bool reportFound = reportsAvail.TryGetValue(reportName, out reportPath);
if(!reportFound)
    reportFound = reports.TryGetValue(reportName, out reportPath);
if(!reportFound)
    reportFound = additionalReports.TryGetValue(reportName, out reportPath);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thought so but "Used to Work" put me off – Muds Mar 24 '15 at 14:51
  • @Muds: it could be that by now `additionalReports` contained the report-name which hides the issue. – Tim Schmelter Mar 24 '15 at 14:53
  • @Muds yeah I remember now that I was only looking up a single report dictionary instead of all three. I've been staring at this window for 2 weeks and had just forgot basics like not overwriting values haha – Brian Crawford Mar 24 '15 at 15:05