none
Generic Dictionary to List<string[]> RRS feed

  • Question

  • I have a generic dictionary, which has id,value,type,section parameters as get; set; in a class, I want to add these values into the List<string[]>. right now I declared one more string array and added those values into the List<string[]> but its overwriting the existing values in the list, please see the below code:-

    public class Parameters
    {

    public int Id { get; set; }
    public string Key { get; set; }
    public object Value { get; set; }

    public string Type { get; set; }
    public string Section { get; set; }
    }
    Dictionary<string, Parameters> _Data;
    List<string[]> _updateList = new List<string[]>();
    _Data = new Dictionary<string, Parameters>();
    _Data = DataFromFile(file); --> this method will fetch the below multiple items
    _Data dictionary will have Key:-( _ROOT_COUNT), Value :- (Key -COUNT, Id-123,value-1,Type-L,Section-_ROOT)
    also this dictionary has 4 count , i mean totally 16 value items will be there.
    I need to assign in List<string[]>. Please let me know best way to assign the string[] List

    Current approach is
    string[] _updateConfigSet = new string[4];
    _updateConfigSet = new string[4];
    for (int index = 0; index < _Data.Count; index++)
    {
    var item = _Data.ElementAt(index);
    var itemKey = item.Key;
    var itemValue = item.Value;
    _updateConfigSet[0] = itemValue.Section.ToString();                                
     _updateConfigSet[1] = itemValue.Key.ToString();
    _updateConfigSet[2] = itemValue.Value.ToString();
    _updateConfigSet[3] = itemValue.Type.ToString();
     _updateList.Insert(index, _updateConfigSet);
    }

    But the list is not updated properly only the last values are in the list, i mean all the 4 items are duplicate values. Please let me know a good approach. Waiting for the replies. Thanks in Advance.

    Tuesday, February 26, 2019 3:50 PM

Answers

  • It's because you have just one array and it's getting overwritten every time. declare the array in the loop so that each time a new array is created with it's own memory allocation.

    Currently there is only one array and it's getting overwritten in a loop. We need everytime a new array as those are all different object.

    for (int index = 0; index < _Data.Count; index++)
    {
    string[] _updateConfigSet = new string[4];  // note this
    var item = _Data.ElementAt(index);
    var itemKey = item.Key;
    var itemValue = item.Value;
    _updateConfigSet[0] = itemValue.Section.ToString();                                 
     _updateConfigSet[1] = itemValue.Key.ToString();
    _updateConfigSet[2] = itemValue.Value.ToString();
    _updateConfigSet[3] = itemValue.Type.ToString();
     _updateList.Insert(index, _updateConfigSet);
    }

    Hope it clears confusion and helps.



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    Tuesday, February 26, 2019 3:54 PM

All replies

  • It's because you have just one array and it's getting overwritten every time. declare the array in the loop so that each time a new array is created with it's own memory allocation.

    Currently there is only one array and it's getting overwritten in a loop. We need everytime a new array as those are all different object.

    for (int index = 0; index < _Data.Count; index++)
    {
    string[] _updateConfigSet = new string[4];  // note this
    var item = _Data.ElementAt(index);
    var itemKey = item.Key;
    var itemValue = item.Value;
    _updateConfigSet[0] = itemValue.Section.ToString();                                 
     _updateConfigSet[1] = itemValue.Key.ToString();
    _updateConfigSet[2] = itemValue.Value.ToString();
    _updateConfigSet[3] = itemValue.Type.ToString();
     _updateList.Insert(index, _updateConfigSet);
    }

    Hope it clears confusion and helps.



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    Tuesday, February 26, 2019 3:54 PM
  • Thank you Ehsan Sajjad. It is working now. marked as an answer too.

    Tuesday, February 26, 2019 6:44 PM
  • That's great, glad to be helpful.

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 27, 2019 5:58 AM