简体   繁体   中英

Populate a string multidimensional or jagged array in a c# class parameter

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;

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM