-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.

2
  • 1
    Those aren't arrays so much as they are dictionaries. Commented Jan 8, 2017 at 5: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. Commented Jan 8, 2017 at 5:41

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

12 Comments

How do i pass this object through a webservice in a string[] object?
It was not mentioned in your question. But, you may use some type of Serialization eg JavaScriptSerializer ... search for it
I am looking this up for weeks now :S cant find anything :(
I suggest ask a new question and exactly explain what you want to do
Perfect This is what i want
|
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%".

13 Comments

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 - 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<string, Dictionary<string, Dictionary<string, string>>>. Are you asking me how to write a function that would return this data?
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 - 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?
YES! Normally it accepts only string[]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.