Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Accessing properties by name
Message
From
26/07/2017 06:35:08
 
 
To
25/07/2017 17:42:13
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01652835
Message ID:
01652849
Views:
55
>>Hi everybody,
>>
>>If I have this class
>>
>>
>>public int LRemoteS1 { get; set; } // LRemoteS1. Layouts.LayoutId of the Salespoint Remote 1 Save
>>        public int LRemoteF1 { get; set; } // LRemoteF1. Layouts.LayoutId of the Salespoint Remote 1 Finalize
>>        public int LRemoteS2 { get; set; } // LRemoteS2. Layouts.LayoutId of the Salespoint Remote 2 Save
>>        public int LRemoteF2 { get; set; } // LRemoteF2. Layouts.LayoutId of the Salespoint Remote 2 Finalize
>>        public int LRemoteS3 { get; set; } // LRemoteS3. Layouts.LayoutId of the Salespoint Remote 3 Save
>>        public int LRemoteF3 { get; set; } // LRemoteF3. Layouts.LayoutId of the Salespoint Remote 3 Finalize
>>
>>I actually have 8 of them and 8 corresponding string properties, is there a way to write code to access them by name in the loop?
>>
>>E.g.
>>
>>
>>for (int i=1; i<=8;  i++ )
>>            {
>>                if (model.LSummary > 0)
>>                    model.LSummaryText = _layoutAdapter.GetLayoutName(model.LSummary);
>>                else
>>                    model.LSummaryText = _layoutAdapter.GetLayoutName(prefssl == null ? 0 : prefssl.DlSummary);
>>            }
>>
>>but instead of the LSummary use LRemoteFi and LRemoteSi instead?
>>
>>Thanks in advance.
>>
>>UPDATE. As I thought, it's not worth the trouble, I'll just copy/paste the same code instead
>>
>>https://stackoverflow.com/questions/2905187/accessing-object-property-as-string-and-setting-its-value
>
>Of course, if it is 1000 then simply copy & paste 992 times more :) I wouldn't really expect this from any programmer. It is quite simple. ie:
>
>
>// var instance = new YourClass();
>// ...
>
>for (int i = 1; i <= 3; i++)
>{
>	var v = instance.GetType().GetProperty("LRemoteF"+i).GetValue(instance); 	// Get
>	instance.GetType().GetProperty("LRemoteF"+i).SetValue(instance, v*100); // Set
>}
>
The ease of copy and paste should not be deciding factor - full ACK.

That said, 6 or 8 values in 2*3 / 2*4 situation with repeating values denormalization code smell for me are not easy to decide.
Iterating and/or deciding between Save and Finalize will need more and slower running code.
If more "repeating" values come into play, copy and paste should not even be considered as it incurs to much cruft/technical debt.

But as the datastructure currently IS denormalized, your coded approach reads to me as:
"I have realized that the data structure is denormalized, have modified the access pattern but was to lazy to change the undelying datastructure to array or list".

As long as the instance class HAS denormalized repeating values, accesing directly (via property name) can be defended in my book.
If the loop really is iterated 8 times, as Naomis loop code suggests, depending on the use case I might switch to a 1..n structure of perhaps

{OtherProperties, {Save,Finalize}[0..7]}
or
{OtherProperties, Save[0..n],Finalize[0..m]},

for which the access pattern will iterate due to structure by default.


my 0.02€

thomas
Previous
Reply
Map
View

Click here to load this message in the networking platform