-1

I want to setup the next code in C#

'price_changes' => array(
    'color' => array(
        'Red' => '2',
        'Blue' => '-10%',
    ),
    'size' => array(
        'Large'  => '+1',
        'Medium'   => '-3',
    ),
),

How do i do this in C# (Winforms)

Or like this:

'price_changes' => array(
    'size' => array(
        'Large'  => '+1',
        'Medium'   => '-3',
    ),
),

Many thanx for the help.

Faith
  • 11
  • 7
  • 1
    Those aren't arrays so much as they are dictionaries. – Abion47 Jan 08 '17 at 05:19
  • Also... in the .Net world, the **types** of your items are extremely important. You don't want to go around setting everything as a string like that. Additionally, instead of a dictionaries or collections of properties, you want to actually define classes for things ahead of time. – Joel Coehoorn Jan 08 '17 at 05:41

2 Answers2

5

Beside the answer posted by Enigmativity which is more like as PHP code, the following code shows using object-properties to achieve similar structs which could be simpler than using those nested Dictionary<string, Dictionary<string, string ... in some situations:

var price_changes = new { 
    color = new { Red = "2", Blue = "-10%" }, 
    size = new { Large = 1, Medium = -3 } 
};

Usage is so easy:

var x = price_changes.size.Large;
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
  • How do i pass this object through a webservice in a string[] object? – Faith Jan 09 '17 at 13:22
  • It was not mentioned in your question. But, you may use some type of Serialization eg JavaScriptSerializer ... search for it – S.Serpooshan Jan 09 '17 at 13:47
  • I am looking this up for weeks now :S cant find anything :( – Faith Jan 09 '17 at 13:55
  • I suggest ask a new question and exactly explain what you want to do – S.Serpooshan Jan 09 '17 at 15:07
  • Also see [this simple code](http://stackoverflow.com/a/6210337/2803565) – S.Serpooshan Jan 09 '17 at 15:21
  • The webservice expects a string[] price_changes as a sting array. So sending the Dictionary is setting up a error. When i want to setup a var like: var str = "{'price_changes':['size':{'XL':'+1','4XL':'-3'}}]}"; the error will be in C#: **Cannot impicitly convert type 'string' to 'string[]'** So the output of the object must be a array of strings. Do you understand now? How do I set the object in C# and pass the new object or the string[] through the PHP web service? I can't change the web service function because it is not changeable. – Faith Jan 09 '17 at 15:32
  • Youe php array is called associative array, a special type which is not an array in c#. Recom you to Search (php web service c# Associative Array) or ask new question – S.Serpooshan Jan 09 '17 at 19:44
  • This did the work. How do i start as a null price_changes and then add the color and sizes programmatically: var price_changes = new { color = new { Red = "2", Blue = "-10%" }, size = new { Large = 1, Medium = -3 } }; – Faith Jan 10 '17 at 13:06
  • Also this does not work: price_changes = new { color = new { Red = "2", Blue = "-10%" }, size = new { 2XL = -10%, Large = 1, Medium = -3 } }; The var does not accepts **2XL** + the **-10%** – Faith Jan 10 '17 at 13:19
  • Hi @S.Serp the code above works great. How do i fill the var dynamicly. Just like I say above: The var does not accepts 2XL + the -10%. Do you know how to fill in the var! – Faith Jan 10 '17 at 13:27
  • in c#, property/variable names can't be started by a number like 2 in 2XL. for dynamic properties, you may use `ExpandoObject` eg `dynamic x = new ExpandoObject(); x.y="10%"; ...` but i'm not sure if you could use it with your php webservice – S.Serpooshan Jan 11 '17 at 05:00
  • 1
    Perfect This is what i want – keroles Monsef Jun 24 '21 at 11:30
1

This would be the C# equivalent:

var data = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>()
{
    {
        "price_changes", new Dictionary<string, Dictionary<string, string>>()
        {
            {
                "color", new Dictionary<string, string>()
                {
                    { "Red", "2" },
                    { "Blue", "-10%" },
                }
            },
            {
                "size", new Dictionary<string, string>()
                {
                    { "Large", "+1" },
                    { "Medium", "-3" },
                }
            },
        }
    },
};

Then if you wrote var blue = data["price_changes"]["color"]["Blue"]; the variable blue would contain "-10%".

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • I have to start as an empty var then fill in the size and color. At last the function must resturn and object or a string[] ? – Faith Jan 09 '17 at 07:52
  • @Faith - I don't understand your comment and your question. There's no empty `var` here. In C# the `var` is a keyword to tell the compiler to infer the actual type. In this case the variable `data` would have the type `Dictionary>>`. Are you asking me how to write a function that would return this data? – Enigmativity Jan 09 '17 at 09:28
  • Hi @Enigmativity, thanx for the response. In order to save our Products table. The function / webservice expects an string[] or a object from the above function / Dictionary. So i cant return the Dictionary. I mannage to output the Dictionary correct now. But the Dictionary needs to convert to a object or a string[] now? – Faith Jan 09 '17 at 09:33
  • @Faith - It is already an object. All things in .NET are. Do you just mean it needs to be serialized into a basic structure that the web service can return? – Enigmativity Jan 09 '17 at 09:39
  • YES! Normally it accepts only string[] – Faith Jan 09 '17 at 10:12
  • @Faith - That's what you need to ask in your question. How are we to know that? Also, how does a PHP associative array get turned into a `string[]`? Is a single JSON string acceptable. You need to make sure you actually ask what you need in the question. – Enigmativity Jan 09 '17 at 10:57
  • Maybe this link is beter to inform: http://www.bubblecode.net/en/2012/04/20/magento-api-associate-simple-products-to-configurable-or-grouped-product/ I want to add the 'price_changes' object with only sizes. – Faith Jan 09 '17 at 11:26
  • @Faith - That link only shows me the PHP code. I can't tell anything else from it. – Enigmativity Jan 09 '17 at 11:50
  • This code is used for adding SKU product id's: 'associated_skus' => array('SKU-001', 'SKU-002') This works fine with the string[] object. But the price_changes var is different. I just don't know how to setup this. 'price_changes' => array( array( 'color' => array( 'Red' => '2', 'Blue' => '-10%', ), 'size' => array( 'Large' => '+1', 'Medium' => '-3', ), ), ), – Faith Jan 09 '17 at 11:55
  • @Faith - Again, that's PHP code. I don't know how that converts to `string[]` so I can't tell you how to do it with C#. – Enigmativity Jan 09 '17 at 12:05
  • var price_changes_sizes = new { color = new { Marine = "-10%" }, size = new { L = "1", M = "-3", S = "-10%" } }; This works good for the webservice. The question now is how do i generate this dynamically? Anyone?? – Faith Jan 10 '17 at 15:28
  • @Faith - You need to ask a new question - no-one but me will get notified about your comment. If you ask a new question then it appears on the SO homepage. Also, your original question has been answered. You didn't ask about web services or conversion to `string[]`. Finally, just saying "This works good for the webservice" doesn't say how it works well - show the code of it working - then we have a chance of showing you how a dynamic solution works. Bottom-line: ask a new question. – Enigmativity Jan 10 '17 at 22:41
  • @Faith - I just saw the other questions you've asked. I have to say that your questions are not very clear and hence aren't good quality questions. You need to really improve how you ask. Have a read of [ask] and [mcve]. – Enigmativity Jan 10 '17 at 22:46