0

I don't expect this to be possible, but it's worth asking.

Let's say, for example, that we have a string like this: myNum = 30

Is there a way to evaluate that, assuming that myNum is a defined variable? I'm making a developer's console for my program and I've seen things like that done before. Is it possible to convert a string into an action that's run by the program? Of course, the program has already been compiled, which is what makes me think this is impossible. I'm trying to avoid a monstrous Select Case/Switch statement, and instead just process commands in a similar way to the way Visual Studio processes conditional breakpoints.

Thanks for your time.

Athena
  • 3,200
  • 3
  • 27
  • 35
  • 1
    Yes. It's called an expression parser. You can write one yourself or you can look for an already written third-party library. You might also consider using CodeDom to build and run the code. – Steven Doggart Mar 27 '14 at 18:42
  • See [this answer](http://stackoverflow.com/a/22454606/1359668) for more info. – Steven Doggart Mar 27 '14 at 18:46
  • The last time I got this 'for free' was with a database (SQLAnywhere) that could evaluate arithmetic expressions. Also with PowerBuilder datawindows. Wouldn't it be nice to have that here, too? – TaW Mar 27 '14 at 19:10
  • Sorry I wasn't clear. I've written expression parsers before. How do you go from a string of myNum to accessing the variable for myNum? – Athena Mar 27 '14 at 19:13
  • 1
    It depends. Is `myNum` a field, a property, a local variable? If it's a field or a property, you could get at it with reflection, but if it's a local variable, that's not an option. Is it possible for you to store these "variables" in a key/value pair list such as a `Dictionary(Of String, Integer)`. – Steven Doggart Mar 27 '14 at 19:16
  • Steven Doggart: +1 for pointing me in the direction of reflection. That's exactly what I needed! – Athena Mar 27 '14 at 19:44

0 Answers0