0

I'm trying to put multiple values ​​(arrays) in a key, but I do not know how to do this.
I've tried:

public class Customer
{
     public string Name { get; set; }
     public string Bought[5] { get; set; }
}

List<Customer> Products = new List<Customer>();

Customer product = new Customer();
product.Bought[0] = listproducts.SelectedIndex;
product.Bought[1] = listproducts.SelectedIndex;
product.Bought[3] = listproducts.SelectedIndex;
product.Bought[4] = listproducts.SelectedIndex;
product.Bought[5] = listproducts.SelectedIndex;

I know this is wrong, but is a poor example for what I'm trying to do.
I was trying something by this way:
Store multiple values in single key in json
But I do not know how to adapt to C#.
I just need to save to the JSON file something like:

[{"Name":"Bryan", "Products":"car", "boat", "bike"}]

If someone is able to help me, I would be grateful. I would give this further step toward knowledge.

Community
  • 1
  • 1
Hypister
  • 147
  • 4
  • 15

3 Answers3

0

You should just use a List<string> for Bought, or List<BoughtObject>.

That will create

{ // Customer
    Name : "Bryan",
    Bought: [// Array of your bought objects 
        { ... },
        { ... }
        ]
}

If you just want it to be a single string comma separated (and then split it on the client side or display it like "Boat, Jet, Tv" you could leave it as type of string and then do a .Join(boughtItems)

John
  • 6,313
  • 3
  • 33
  • 56
  • I'm not really knowing how to do this. To add just one product from a textbox, for example , I use product.Bought = textboxProductName.Text and Products.Add (product). This works well and adds one product in the key. But I am wanting to learn how to add, for example, a product selected using one checkbox and click on the "Add" button. If I have the corresponding checkbox for the bike, the checkbox for the car, the checkbox for TV, etc. When I check the checkboxes, the values goes to the product key for the current customer. I wonder how to add them in the same key with comma-separated. – Hypister Oct 16 '14 at 03:06
0

You could use something like Dictionary<Type1, List<type2> >. In your case you could do Dictionary<string, List<string> >. That way you will able to store multiple values for a key.

itachi
  • 192
  • 2
  • 10
0

Try with this may be it will work

                  public class Customer
                  {
                  public string Name { get; set; }
                  public readonly string[] Bought = new string[5];
                  }
Midhun Mundayadan
  • 3,106
  • 3
  • 18
  • 32