Bug 21094 - Dictionary adding rather than inserting
Summary: Dictionary adding rather than inserting
Status: RESOLVED INVALID
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-07-03 10:59 UTC by Paul Johnson
Modified: 2014-07-04 06:41 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Paul Johnson 2014-07-03 10:59:03 UTC
Using this with Xam.iOS on the alpha channel (so built against mono 3.6)

Consider the following code

var five = new Dictionary<int, char>();
                    for (int i = fiveback.Year; i <= lastyear.Year; ++i)
                        five.Add(i, ' ');
                    foreach (var cr in cropDetails)
                    {
                        var yrList = cropDetails.Where(t => t.Paddock == cr.Paddock).Select(t => t.Year).ToList();
                        foreach (var y in yrList)
                        {
                            if (five.ContainsKey(y.Year))
                            {
                                var yr = five.Keys.FirstOrDefault(t => t == y.Year);
                                int r = 0;
                                for (int i = fiveback.Year; i < lastyear.Year; ++i)
                                {
                                    if (i != yr)
                                        r++;
                                    else
                                    {
                                        five[r] = cr.CropCode;
                                        break;
                                    }
                                }
                            }
                        }
                        cropDict.Add(cr.Paddock, five);
                        five.Clear();
                    }

(I'm using this as OrderDictionary isn't available - will file that as a different bug)

The loop is simple - I test to see if the key is contained, if it is, iterate again and fine where in the dictionary it is. Once found, I insert as I would in an array (following how it's done here http://stackoverflow.com/questions/1243717/how-to-update-the-value-stored-in-dictionary-in-c).

Instead of inserting at the correct position, a new dictionary entry is added at the end

lastyear = DateTime.Now.AddYears(-1), five years is DateTime.Now.AddYears(-5)
Comment 1 Paul Johnson 2014-07-03 11:03:44 UTC
OrderedDictionary bug : #21095
Comment 2 Marek Safar 2014-07-04 06:41:14 UTC
You cannot insert at specific position in dictionary