2

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.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 2
    type `prop` then press tab – Hamid Pourjam Mar 03 '16 at 13:24
  • I am new to `C#`, I apologize if this Question isn't in the best of a description. I am not understanding what your comment is referring to? Thanks for your input however, @dotctor – Jaquarh Mar 03 '16 at 13:26
  • Oh, I just tired that and it automatically did the `get; set;` for me. Lol, thats awesome! – Jaquarh Mar 03 '16 at 13:27
  • How is that example code *less* "long winded" than simple properties? It's not clear to me what you're even trying to *do* there. – David Mar 03 '16 at 13:27
  • Because in PHP, you can simple create a 3D multidimensional Array which you can then use something similar to this: `(foreach $array as $k=>$v){}` to handle the data - which yes, makes it a lot easier which less code. @David – Jaquarh Mar 03 '16 at 13:28
  • 2
    @KyleE4K: "Shorter code" and "simpler code" aren't always synonymous. That does make sense in PHP but not so much in C#, being a very different language. – David Mar 03 '16 at 13:32
  • @KyleE4K PHP arrays aren't strongly typed, C#'s are, unless we get into `dynamic` taboo. Are you looking for a [`Dictionary`](https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx)? – Gediminas Masaitis Mar 03 '16 at 13:32
  • I am just looking for something that corresponds to that ideology, any references would be perfect. @GediminasMasaitis – Jaquarh Mar 03 '16 at 13:33
  • So basically, nested Arrays are not implementable inside C#? @David Excuse the word if its made up but you get my Gist - I hope haha. – Jaquarh Mar 03 '16 at 13:38
  • @KyleE4K: Nobody said that. Arrays can contain arrays. What does that have to do with class properties? You seem to be confusing two different things, making the question unclear. – David Mar 03 '16 at 13:40
  • Because the idea is to switch the class properties with nested arrays - but that's why I am asking because I am unsure a) how to do that and b) if its even possible in the first place. @David – Jaquarh Mar 03 '16 at 13:41
  • 2
    @KyleE4K: Properties themselves and the *types* of those properties are two different things. And using untyped arrays to store class structures is a *famously* bad idea in languages like C# and Java. There exist refactoring patterns specifically to move away from doing that. – David Mar 03 '16 at 13:43
  • It seems such a prolonged sequence when using C# to do the things you can do in other languages which give the same product. I really appreciate these comments though, if you want to add that as an answer - I will mark it. @David Else, I'll close it. – Jaquarh Mar 03 '16 at 13:44
  • 1
    @KyleE4K: It doesn't really *answer* the question, and I suspect it's because the question is just off the mark of what's really being addressed. Presumably you have some code in C# which you don't think is "clean" or maintainable in some way, and you want to make it "better". You've assumed one way to do it, based on a different language, but that way doesn't make much sense here. While code reviews themselves are off-topic here, you might find some help refactoring C# code from things like Martin Fowler's Refactoring Patterns, or Robert Martin's "Clean Code" book. – David Mar 03 '16 at 13:53

2 Answers2

4

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.

Gediminas Masaitis
  • 3,172
  • 14
  • 35
  • See, I've spent 4 years in PHP. Recently got an apprenticeship in C# and its just so crazy seeing how much longer C# is.. I still don't understand why people are not using `Hack` because that uses, like you say, a **weakly typed** language but has the optimized performance of a `C` language. These C# Controllers, Handlers and Instances are so confusing to get at :/ Thanks for helping me understand though! – Jaquarh Mar 03 '16 at 14:00
  • @KyleE4K Longer isn't necessarily worse, writing the most compact code shouldn't be a goal, writing the most maintainable and readable code should be. Lots of languages have different ideologies, you can see it like: When in Rome, do as Romans do... – Icepickle Mar 03 '16 at 14:03
  • Gediminas, wouldn't it make more sense to set the dictionary as `IDictionary` seeing that not only strings go in there? And then it can be easy to add a new Dictionary as a value for any properties? Although I highly agree on your critical remarks ;) – Icepickle Mar 03 '16 at 14:05
  • This does what all that does and is maintainable and readable: http://pastebin.com/xHZDH3h4 @Icepickle convert that to `Hack` and your performance is boosted and page load is average 2/3seconds. This `C#` is so confusing xD but thanks! - sometimes the slowest don't always win the race :) – Jaquarh Mar 03 '16 at 14:06
  • 1
    @Icepickle I wouldn't say so, as then you would need to cast the `object` back to the original type whenever you wish to use the values stored. Now, you *could* consider `IDictionary`... But let's not. – Gediminas Masaitis Mar 03 '16 at 14:08
  • True, however now you would have to cast when filling the dictionary :D I guess it depends on how one would use such a system afterwards. Good point, and yes, lets stay away from the dynamic idictionary thingie :) – Icepickle Mar 03 '16 at 14:13
0

I suggest to look how ViewBag used in MVC works

here a good link

How ViewBag in ASP.NET MVC works

Community
  • 1
  • 1