2

I can't for the life of me figure out why the legacy code is doing this:

HashSiteMapping.Add(""+sm.SiteNumber, sm.LocationNumber);

...when this seems more sensible:

HashSiteMapping.Add(sm.SiteNumber, sm.LocationNumber);

I would just shrug my shoulders and chock it up to the general bizarreness of this code, but I'm getting "value does not fall within the expected range" in this code and am wondering if that might be the problem. In fuller context, the code was this:

IEnumerator en = mFile.Map.Mappings.GetEnumerator();

while (en.MoveNext())
{
    SiteMapping sm = (SiteMapping) en.Current;      
    HashSiteMapping.Add(""+sm.SiteNumber, sm.LocationNumber);
}

...and I changed it to this:

IEnumerator en = mFile.Map.Mappings.GetEnumerator();

while (en.MoveNext())
{
    SiteMapping sm = (SiteMapping) en.Current;      
    if (!HashSiteMapping.Contains(sm.SiteNumber))
    {
        HashSiteMapping.Add(sm.SiteNumber, sm.LocationNumber);
    }
}

...but I still get, "value does not fall within the expected range"

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • HashSiteMapping is a simple HashTable. SiteNumber is an int. – B. Clay Shannon-B. Crow Raven Apr 03 '13 at 00:01
  • The prepended empty string has nothing to do with it- that will be eliminated by the compiler. – Chris Shain Apr 03 '13 at 00:13
  • 2
    What line is the error on? The Add? Also, adding the empty string is making that parameter into a string, without it that parameter will be typed as an int. – Malcolm O'Hare Apr 03 '13 at 00:23
  • 1
    It would be helpful if you told us what line of code is giving that error, and if it's an exception (and what specific exception). – Jim Mischel Apr 03 '13 at 00:31
  • @Jim Mischel: "Value does not fall within the expected range" is the exception. The line number? I have no idea - I have to narrow down where exceptions are occurring by sprinkling MessageBox.Show()s throughout and then seeing which is the last one reached, and then I know it's blowing up after that and before the next MessageBox.Show(). Yes, it's extremely tedious and painful. – B. Clay Shannon-B. Crow Raven Apr 03 '13 at 15:14

2 Answers2

1

I am not familiar with HashSiteMapping. Is this a custom class? Can you provide a link or defining code?.

My best guess is there are instances where your SiteMapping SiteNumber is null or empty. In that case, your original code would be doing something like this:

IEnumerator en = mFile.Map.Mappings.GetEnumerator();

while (en.MoveNext())
{
    SiteMapping sm = (SiteMapping) en.Current;
    if (String.IsNullOrEmpty(sm.SiteNumber))
    {
      HashSiteMapping.Add(String.Empty, sm.LocationNumber);
    }
    else
    {
      HashSiteMapping.Add(sm.SiteNumber, sm.LocationNumber);
    }
}

Notice that even though the code above has no comments, what is going on is still easier to understand than the mystery code you were left to work with.

The 2nd version you tried (shown below) could be failing because the SiteNumber and LocationNumber together both create a type of composite key (again I'm guessing because I don't know the HashSiteMapping definition)

while (en.MoveNext())
{
  SiteMapping sm = (SiteMapping) en.Current;        
  if (!HashSiteMapping.Contains(sm.SiteNumber))
  {
    HashSiteMapping.Add(sm.SiteNumber, sm.LocationNumber); // What if LocationNumber has not been included yet?
  }
}

Consider:

|_SiteNumber__|_LocationNumber_|
| "Warehouse" |      "A1"      |
| "Warehouse" |      "B1"      |
|     NULL    | "Boss'sOffice" |
|     NULL    |"JanitorCloset" |
|     NULL    |   "RestRoom"   |

You certainly would not want to get those all confused!

Hope that helps.

0

You may want to hook in an assert in debug build and watch for values that violate something defined as "expected range" for the HashSiteMapping class--