none
C# Change value in dictionary RRS feed

  • Question

  • Hi everyone, 

    I was wondering if someone could help me with an issue that has been bugging me for a while.

    I have a dictionary with only 4 items in it. 3 of which have {{}} surrounding them, and these items need to be changed.

    the issue I have is that I am not sure how to change multiple values at one time in a foreach statement.

    I can change one value but I am not sure how to re-write multiple changes to a file.

    here is my code:-

    public static void Diction1()
            {
                Dictionary<string, string> diction = new Dictionary<string, string>();
                diction.Add("{{name}}", "{{dan}}");
                diction.Add("{{work}}", "{{software}}");
                diction.Add("{{car}}", "{{honda}}");
                diction.Add("channel", "2");

                string text = string.Join(Environment.NewLine, diction.Select(d => d.Key + "," + d.Value));
                File.WriteAllText(@"C:\EDchannel2\EDchannelTemplate.txt", text);// Writes to a template file.

                string readalltext = File.ReadAllText(@"C:\EDchannel2\EDchannelTemplate.txt");

                foreach (KeyValuePair<string, string> pair in diction)
                {
                    string result = readalltext.Replace("{{dan}}", "monkey");

                    string outputFile = @"C:\EDchannel2\EDchannel1.txt";
                    File.WriteAllText(outputFile, result); //Writes results to a file.
                }
            }

    If anyone has any ideas, please help.

    thank you.

    regards

    Dan

    Wednesday, February 14, 2018 2:13 PM

All replies

  • Hello,

     All depends on changes to the Dictionary Class and the output file coding.

    If nothing changes then you can method ReadAllLines() into String[] array.

    Then do a search/replace in memory so you can write the new data to the

    output file.  If you want to search/replace using the Dictionary Class then

    keep using the ForEach with KeyValuePair . Inside the loop us IF to evaluate

    the Key results to True then change the Value for that Key. Remember, you

    can not add/remove Keys from Dictionary Class but can change Values while

    the ForEach loops execute.

     

     Hope this helps :)

    Wednesday, February 14, 2018 3:14 PM
  • Maybe like this:

       string result = File.ReadAllText(@"C:\EDchannel2\EDchannelTemplate.txt");
       result = result.Replace("{{dan}}", "monkey");
       result = result.Replace("{{software}}", "something");
       . . .
       File.WriteAllText(outputFile, result);

    Wednesday, February 14, 2018 5:33 PM
  • Think of the following:

    diction.Add("{{name}}", "{{dan}}");
    diction.Add("{{work}}", "{{software}}");
    diction.Add("{{car}}", "{{honda}}");
    diction.Add("channel", "2");

    As saying:

    {{name}} = "{{dan}}"
    {{work}} = "{{software}}"
    {{car}} = {{honda}}
    channel = "2"

    The first strings, the ones on the left, are the keys. The ones on the right are the values. You can change the value for a key but you can't change the key. So you cannot change {{dan}} to "monkey".

    Another problem is that when reading the data back in, you need to convert it back to a dictionary and you are not doing that.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, February 14, 2018 7:09 PM

  • the issue I have is that I am not sure how to change multiple values at one time in a foreach statement.


    Why would you do that?

    If you do it in 3 times it takes probably 0.00000000000000000000000000000001 second more time. 


    Success
    Cor

    Wednesday, February 14, 2018 7:15 PM
  • This looks likw what I am searching for, do I place that within my foreach block Viorel_?

    Thursday, February 15, 2018 6:48 AM
  • Makes sense Sam thank you for your feedback and help.
    Thursday, February 15, 2018 7:45 AM
  • ok cool, thank you so much for your help User3DX. I will have a play around with the coding and see where I get. Thanks again
    Thursday, February 15, 2018 7:46 AM
  • Hi, you have helped me a lot with this answer. Thank you so much.

    Dan Boddington

    Monday, February 26, 2018 6:55 AM
  • Dictionary<string, string> diction = new Dictionary<string, string>();
    diction.Add("{{name}}", "{{dan}}");
    diction.Add("{{work}}", "{{software}}");
    diction.Add("{{car}}", "{{honda}}");
    diction.Add("channel", "2");
    
    Dictionary<string, string> copy = new Dictionary<string, string>(diction.Count);
    foreach (var item in diction)
    {
    	if (item.Value.Contains("{{dan}}"))
    	{
    		copy.Add("foo", "bar");
    	}
    	else
    	{
    		copy.Add(item.Key, item.Value);
    	}
    }
    diction = copy;
    is this ok?
    Monday, February 26, 2018 1:23 PM