Home > Back-end >  C# Dictionary loses keys and values
C# Dictionary loses keys and values

Time:07-11

i have a problem with updating keys and values in dictionary, dictionary loses data everytime when new StreamReader runs on. I wanted StreamReader to read list and when it reads second path keys and values in dictionary are already lost. I would be greateful for help.

class TT_connect
{
   public Dictionary<string, double> s_c = new Dictionary<string, double>();
    public TT_connect(List<string> tempAS){
        Dictionary<string, double> s_a = new Dictionary<string, double>();
       foreach(string a in tempAS)
        { 
            using (var s = new StreamReader(a))
            {
                while (!s.EndOfStream)
                {
                    var l_a = s.ReadLine();
                    var l_b = l_a.Split(';');
                    if (s_a.Keys.Contains(l_b[0]))
                    {
                        double.TryParse(l_b[1], out double l1);
                        s_a[l_b[0]]  = l1;
                    }
                    if (!s_a.Keys.Contains(l_b[0]))
                    {
                        double.TryParse(l_b[1], out double l2);
                        s_a.Add(l_b[0], l2);
                    }
                }
            }
        }
        foreach(KeyValuePair<string,double> s in s_a)
        {
            s_c.Add(s.Key, s.Value);
        }
        } 
}

CodePudding user response:

Here's what I recommend:

  1. Instantiate your class's dictionary field in the class constructor. That's all you should do in the constructor. Or, go ahead and let it instantiate automatically, as you do now, and forego the constructor entirely.

  2. Provide an Add method that replaces the code in your previous constructor. Use it to do the work of adding your dictionary/stream items. You won't need another dictionary; just add the items to your class's dictionary, directly.

Your current logic does a bit too much in the constructor, and overall there is more logic in your class than you actually need to get the job done.

CodePudding user response:

Based on Roberts comments i have tried to clean up the implementation a bit.

class TT_connect
{
    private string _fileLocation;
    public Dictionary<string, double> dict;
    

    public TT_connect(string fileLocation)
    {
        dict = new Dictionary<string, double>();
        _fileLocation = fileLocation;
    }

    public void FillDictionary()
    { 
        using (var s = new StreamReader(_fileLocation))
        {
            int lineCount = 0;
            while (!s.EndOfStream)
            {
                string line = s.ReadLine();
                lineCount  ;

                string key = line.Split(';')[0];
                string stringValue = line.Split(';')[1];

                if (!Double.TryParse(stringValue, out double val))
                    throw new Exception($"Can't pass value on line: {lineCount}");

                if (dict.Keys.Contains(key))
                {
                    dict[key]  = val;
                }
                else
                {
                    dict.Add(key, val);
                }
            }
        }
    }
}
  •  Tags:  
  • c#
  • Related