I have strings like following:
"[Current.Age] - 10"
"[Current.Height] + 50"
"[Current.Age] + 10 - [Current.Height] - 50"
And I’d like to replace [Current.Something] with the numerical value of the current selected object, for example a selected object may have the following state:
var student = new Student();
student.Age = 20;
student.Height = 180;
So, the strings should end up like:
"20 - 10" * or better * "10"
"180 + 50" * or better * "230"
"20 + 10 - 180 - 50" * or better * "-200"
I think I should use regular expressions for this. Any ideas as to how I can accomplish this?
Edit: What I need is pretty much something that can take [Current.Something]s and replace them with relevant values. I know I can do it with simple string operations but I just wonder if there’s a short way to do this.
If you have control of the class that contains the value; you can just add a method called:
Then you need to run the text through a text parser (you should be able to create onc fairly easily).
Something like:
There would be more work for determining what to do for connected statements, but the above should get you going in the right direction. There are slightly cleaner ways to do this using reflection but it’s harder to maintain.