Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Accessing properties by name
Message
From
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:
01652840
Views:
63
This message has been marked as a message which has helped to the initial question of the thread.
>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
}
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform