1

I'm pretty much new to c# and I'm having trouble populating a multidimensional or jagged array (I'm not even sure which one I need tbh).

This is the code I have so far

class CustomFields
{
    public Array vehicle_options { get; set; }
}

var custom_fields = new CustomFields();
// options_list is populated from an external web service
List<Options> options_list = new List<Options>(); 
List<string> options = new List<string>();

foreach (Option option in options_list)
{
    options.Add(option.name.Value); //Option name is a string
}
    custom_fields.vehicle_options = options.ToArray();

With this code I'm getting an array that looks like this:

["vehicle_options"] => 
    [0] => "Some value",
    [1] => "Some other value",
    ...

This is what I'm trying to achieve

["vehicle_options"] => 
    [0] => 
        ["vehicle_option"] => "Some value",
    [1] =>
        ["vehicle_option"] => "Some other value",
    ...

So basically I'm trying to set a key for all option name values. And it will always be "vehicle_option".

I'm pretty sure I need to declare my array differently and add the values differently than what I tried so far... I tried many different approaches, but just can't seem to get it working...

EDIT

options_list is a list of "vehicle option objects" that I'm getting from an external source. These "option objects" have a name property which I am extracting and using them to populate the array.

Any tips on how I can achieve this (using as much of the existing code as possible)?

EDIT2

This is my "fix" attempt for @Snapshot's answer:

class CustomFields
{ 
public Dictionary<string, string>[] vehicle_options { get; set; }
}

Dictionary<string, string>[] vehicleOptions = new Dictionary<string, string>[]()
{
    new Dictionary<string, string>()
    {
        {"vehicle_option", "Value1"},
    },
    new Dictionary<string, string>()
    {
        {"vehicle_option", "Value2"},
    },
    new Dictionary<string, string>()
    {
        {"vehicle_option", "Value3"},
    }
};
custom_fields.vehicle_options = vehicleOptions;
7
  • what is options_list ? Commented Mar 31, 2018 at 13:46
  • Is a list of options what you need? Commented Mar 31, 2018 at 13:52
  • @zackraiyan check my edit please. Commented Mar 31, 2018 at 14:07
  • your code still doesn't contain options_list , what is it ?? and what is ur goal/expected result ? Commented Mar 31, 2018 at 14:08
  • @zackraiyan I'm not sure that options_list is relevant, it's basically just a list of strings (or more correctly a list of objects with a string parameter). My expected result is to get a final array that looks like the one I mentioned in the "This is what I'm trying to achieve" section. Basically all I need is to insert the "vehicle_option" key (which is a fixed string) as the key for all extracted vehicle option name values. Commented Mar 31, 2018 at 14:27

1 Answer 1

2

It looks like you are trying to use your array in a way that would seem reasonable for a Dictionary<string, string> or a Dictionary<string, object>.

class CustomFields
{
    public Dictionary<string, string> VehicleOptions { get; set; } = new Dictionary<string, string>();
}

var custom_fields = new CustomFields();
foreach (Option option in options_list)
{
    custom_fields.VehicleOptions.Add(option.name.Value, "default value"); //Option name is a string
}

you could then access a specific option by using its name

var height = custom_fields.VehicleOptions["Height"];

or iterate over the whole dictionary in case you do not know the possible options before hand.

foreach (var optionsKeyValue in custom_fields.VehicleOptions)
{
    Console.WriteLine($"Name: {optionsKeyValue.Key} Value: {optionsKeyValue.Value}");
}

Edit

Would you mind testing if the following would return the correct result?

Dictionary<string, Dictionary<string, string>[]> vehicleOptions = new Dictionary<string, Dictionary<string, string>[]>()
{
    {"vehicle_options", new Dictionary<string, string>[]
        {
            new Dictionary<string, string>()
            {
                {"vehicle_options", "Value1"},
            },
            new Dictionary<string, string>()
            {
                {"vehicle_options", "Value2"},
            },
            new Dictionary<string, string>()
            {
                {"vehicle_options", "Value3"},
            }
        }
    }
};

Edit 2 Assuming the structure is dependant on your already existing class CustomFields.

class CustomFields
{
    public List<Dictionary<string, string>> vehicle_options { get; set; } = new List<Dictionary<string, string>>();

    public void AddVehicleOption(string value)
    {
        var options = new Dictionary<string, string>();
        options.Add("vehicle_options", value);
        vehicle_options.Add(options);
    }
}

var custom_fields = new CustomFields();
foreach (Option option in options_list)
{
    custom_fields.AddVehicleOption(option.name.Value); //Option name is a string
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, but seems that the "key" is set as the "value", while the "value" is set to "default value" with your code. What I really need is to have the key fixed as "vehicle_option" and the "value" to be dynamic based on options_list. I tried to switch positions of option.name.Value and default value, but seems that a duplicate key exception occurs. Thank you for the effort though... If you have any other idea, would be appreciated!
I tried it and it seems close, but still not fully correct unfortunately. The problem seems to be that the "vehicle_options" key isn't necessary because the original class "custom_fields" contains a parameter "vehicle_options" which I am filling with [0][vehicle_option][value1], [1][vehicle_option][value2]. So it seems that you went "a step too far". I tried adjusting it, but got syntax errors (I'm new to c#...). I'll post the latest code in a question edit, maybe you can spot the error?
Works great! Thanks a million!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.