Jaquarh
Jaquarh

Reputation: 6683

C# - Array Infrastructure in a Class using Controls

I am just curious whether or not this can be achieved: I have currently got a sort of problem where I am using controllers to set and get data inside a Class and this is done through methods and instances on the handler side.

I am just wondering, because doing public string x { get; set; } can become very long winded for each property that your project consists of.

Is there a way that I can achieve this sort of ideology inside a Class?

public core Array[] (
          Option1 => string Array[] (
             Name => 'example'
          ),
          Option2 => String Array[] (
             Name => 'example2'
          ) { set; get; }
);

Of course, this is just a theory and won't be the exact solution. I am wondering if I'd:

1) Need a controller when appending data to the index's.
2) Need to instance the Class that the Controller handles or if I can do it through Main Class methods.
3) Need a multidimensional Array or List.

My current solution is to long winded and due to the large amount of Data the Core site uses, It's response is descending for every feature being added.

Could anyone reference any infrastructure references or possibly give a walk through on how to actually allocate the properties using this ideology?

Thank-you in advance.

Edit: I mean something like this (PHP):

$example = array (
    'location1' => array(
        'location_id' => 1
    ),
    'location2' => array(
        'location_id' => 2
    )
);

Now the data can be handled easier by:

foreach($example as $k=>$v){ // todo: handle }

So here $k becomes your array name (ie it could be, Name, Address) and $v becomes your nested array making data so much easier to handle.

Upvotes: 2

Views: 87

Answers (2)

Othello  .net dev
Othello .net dev

Reputation: 438

I suggest to look how ViewBag used in MVC works

here a good link

How ViewBag in ASP.NET MVC works

Upvotes: 0

Gediminas Masaitis
Gediminas Masaitis

Reputation: 3212

While I strongly disagree with the usage of this pattern, I still think it's valuable to know.

I think you are looking for a Dictionary<TKey, TValue>. It provides a way to map keys of any type to values of any type. For your use case:

IDictionary<string, string> DynamicProperties {get; set;} = new Dictionary<string, string>
{
    { "FirstName", "John" },
    { "LastName", "Doe" }
};

You can then iterate over your "properties" with a loop:

foreach(KeyValuePair pair in DynamicProperties)
{
    string key = pair.Key; // "FirstName", "LastName"
    string value = pair.Value; // "John", "Doe"
    // Use them as you wish.
}

You can have dictionaries of dictionaries too. To match your updated example:

IDictionary<string, IDictionary<string, int>> Example {get; set;} = new Dictionary<string, IDictionary<string, int>>
{
    {"location1", new Dictionary<string, int> {{"location_id", 1}}},
    {"location2", new Dictionary<string, int> {{"location_id", 2}}}
};

But look at this code - you were looking for simplicity. This is not simple at all, nor is it short, clear, or testable. Having classes and properties is the way to go in C#.

I think the root of the problem here is that you are coding with C#, but thinking with PHP ideas. C# is strongly typed, while PHP is weakly typed, (see this wiki article), and you need to readjust your thinking appropriately.

Upvotes: 4

Related Questions