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;
options_list?options_list, what is it ?? and what is ur goal/expected result ?