XXYY
XXYY

Reputation: 3

Values are being overwritten with each key addition in dictionary

public Dictionary<string, List<int>> GetAllMemberIDsWithinATable()
{
    List<string> _sectionDataList = this.GetSectionInfo();
    var dict = new Dictionary<string, List<int>>();
    List<int> _memberIds = new List<int>();
    string _staadName = "";
    int iVal = 0;
    bool isEnd = false;

    for (int iSecList = 0; iSecList < _sectionDataList.Count(); iSecList++)
    {
        while ((iSecList < _sectionDataList.Count()) && 
               (!_sectionDataList[iSecList].Equals("TABLE")))
        {
            string data = _sectionDataList[iSecList];
            if (!(data.Equals("TO") || (data.Equals("-"))))
            {
                if (data.Equals("SP") || data.Equals("0.01"))
                {
                    isEnd = true;
                    break;
                }
                else if (!_memberIds.Contains(Convert.ToInt32(data)))
                    _memberIds.Add(Convert.ToInt32(data));
            }                    
            else
            {
                int jData = Convert.ToInt32(_sectionDataList[iSecList + 1]);
                if (iSecList != 0)
                    iVal = iSecList - 1;
                int xData = Convert.ToInt32(_sectionDataList[iVal]);
                for (int i = xData; i <= jData; i++)
                    if (!_memberIds.Contains(i))
                        _memberIds.Add(i);
            }
            iSecList++;
        }

        if (iSecList != _sectionDataList.Count()) 
        {
            if (!isEnd)
            {
                iSecList = iSecList + 2;
                _staadName = _sectionDataList[iSecList];
                this.staadName = _staadName;
                dict.Add(_staadName, _memberIds);
                _memberIds.Clear();
            }
        }
    }
    return dict;
}

_memberIds are getting replaced while new key i.e. _staadname addition. like I want to have TUB1501506,ISMC250 etc as my key and fetching them from a file and storing data in memberIds from

"43 TO 62 71 72 82 92 101 TO 105 110 TO 112 137 TO 146 156 157 168 171 173 - 174 185 TO 188 197 198 218 220 TO 222 240 241 244 256 272 274 275 - 280 TO 282 285 TO 290 294 TABLE ST TUB1501506 63 TO 69 73 TO 77 79 TO 81 83 TO 87 89 TO 91 93 TO 100 108 109 113 TO 117 - 119 TO 136 189 TO 195 206 209 TO 215 217 223 225 TO 239 245 253 264 278 291 - 293 297 TABLE FR ISMC250 1 TO 4 7 TO 22 25 TO 28 31 TO 42 147 TO 155 158 TO 166 175 TO 183 292 - 299 TABLE FR ISMC200 SP 0.01 "

As you can see the pattern is the last string is the _staadname and the range give before the string are the list of memberIds for that name.

Upvotes: 0

Views: 157

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156948

That is simply because you only have one instance of _memberIds. You never re-instantiate it inside the loop. Hence, your list is reused every iteration of the for loop.

Try to move it inside the loop:

// not here

for (int iSecList = 0; iSecList < _sectionDataList.Count(); iSecList++)
{
    // put it here
    List<int> _memberIds = new List<int>();

    // your original code
}

Upvotes: 1

Related Questions