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.