Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

C# Calling data member using a concatenated string

Hello,

I asked this question earlier in Javascript, but now I can't get it to work in C#.  I am looking to do something like below:

currentPart = results[0]["orderPart" + i];

Open in new window


Results is an object of a class.  In this class there are 5 members named:
orderPart1
orderPart2
... orderPart5

I am looking to update these 5 members by using a for loop to iterate the last character of the data member name.  I have tried using the dot operator and other symbols.  I have checked C and C++ options also, with no success.

Any help is appreciated.  Thank you.
0
paulpp
Asked:
paulpp
  • 3
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You would need to use REFLECTION.  Is "orderPart1" just a public/private Member, or is it an actual Property?
0
 
paulppAuthor Commented:
Thank you both for the information - I will look it up and see if it solves my problem.

@Idle_Mind - orderPart1 is a public member.

Thank you.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It will be very similar to my example, then, except you'd call GetValue() instead of SetValue():
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue.aspx
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now