0

How do I pass in a class property to a method and have it assigned a value?

For example, below is a simple scenario. The inner workings of the method will be a little more complex.

Action <string> MyMethod = (myString) =>
{
  myString = "testing...";
};

and then call it this way

MyMethod (someClass.String1);
MyMethod (someClass.String2);
MyMethod (someClass.String3);

Such that each of the three properties retain an assigned value from the method. As it is, none of the properties will retain a value. They will be null.

I don't want to pass the class in because that means the method will need logic to figure if it is assigning to String1, String2, or String3. I'd like it to remain very generic.

4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • Must your Action be of that specific definition? – Ron.B.I Jul 09 '13 at 20:54
  • I would like to keep this very concise, inline and not have to split this into several constructs. – 4thSpace Jul 09 '13 at 20:57
  • I do not know of a way to declare delegates inside methods so I removed my answer... – chancea Jul 09 '13 at 21:04
  • I think you would have to iterate over the class properties? http://stackoverflow.com/questions/8151888/c-sharp-iterate-through-class-properties – Dom Jul 09 '13 at 21:41
  • This requires knowledge about each specific property and moves away from a more generic approach. – 4thSpace Jul 10 '13 at 17:46

0 Answers0