0

I am writing a program in which i want Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();

I have 32 buttons and every button has a name "Btn00" to "Btn32". I am changing color of buttons on click, and i want to have dictionary which will contain button name and list of colors. Then I will parse to json to have {"Btn00":[0,0,0],"Btn01":[255,0,255], etc.}. I have sliders with which I change color on click:

List<int> btnColors = new List<int>();
public void changeColor(Button button)
    {
        byte rr = (byte)SeekR.Value;
        byte gg = (byte)SeekG.Value;
        byte bb = (byte)SeekB.Value;
        Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
        SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
        button.Background = colorBrush; //Setting background of a button.

        btnColors.Add(rr);
        btnColors.Add(gg);
        btnColors.Add(bb);

And I call changeColor function here:

private void changeBtnColor(object sender, RoutedEventArgs e)
    {
        changeColor(sender as Button);
    }

I have tried making

String btnName;
Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();

but I don't know how can i put button names as string in dictionary, and then parse it to JSON so I would have above notation.

EDIT:

I changed my code like this:

public void changeColor(Button button)
    {


        List<int> btnColors = new List<int>();
        Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();
        byte rr = (byte)SeekR.Value;
        byte gg = (byte)SeekG.Value;
        byte bb = (byte)SeekB.Value;
        Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
        SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
        button.Background = colorBrush; //Setting background of a button.
                                        //ledIndBack.Background = colorBrush;

            btnColors.Add(rr);
            btnColors.Add(gg);
            btnColors.Add(bb);

            nameColor.Add(button.Name, btnColors);


        string json = JsonConvert.SerializeObject(nameColor);
        Console.WriteLine(json);
    }

And I get json values like {"Btn00":[255,0,0]} {"Btn10":[0,0,255]} etc. So it is not in the same object, and I probably need to use loop and SerializeObject outside the loop and I tried but every time I get the same response. Also, these codes are in my xaml.cs file. Is that going to be a problem, or should I add Commands and use functions in my ViewModel folder -> BtnViewModel.cs. I also have to send my data to the server, and I don't know how smart is it to do everything in xaml.cs.

jnkvcu
  • 11
  • 3
  • 1
    `nameColor[button.Name] = btnColors;` Beware that you are reusing btnColors, so you will append the values each time you call it, you should create a new `List` on each call to `changeColor`. – Gusman Jun 15 '20 at 18:13
  • Does this answer your question? [C# JSON Serialization of Dictionary into {key:value, ...} instead of {key:key, value:value, ...}](https://stackoverflow.com/questions/4861138/c-sharp-json-serialization-of-dictionary-into-keyvalue-instead-of-keyk) – Ryan Wilson Jun 15 '20 at 18:19
  • I think at this moment, I just have to somehow store the colors in the loop. – jnkvcu Jun 15 '20 at 19:46

1 Answers1

0

Make a dictionary of tuples instead of a dict of strings and then parsing the strings
Dictionary<int, (int r, int g, int b)>
https://learn.microsoft.com/en-us/dotnet/csharp/tuples

Since you only have 32 values, you can also make switches. There is an example of this on the docs. https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

public static RGBColor FromRainbow(Rainbow colorBand) =>  
    colorBand switch  
    {  
        Rainbow.Red    => new RGBColor(0xFF, 0x00, 0x00),  
        Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),  
        Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),  
        Rainbow.Green  => new RGBColor(0x00, 0xFF, 0x00),  
        Rainbow.Blue   => new RGBColor(0x00, 0x00, 0xFF),  
        Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),  
        Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),  
        _              => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
    };  

However, I would still go with the tuples; it's more flexible than switches.

Jeff
  • 810
  • 8
  • 18