Question

Is it possible to get Visual Studio to do mathematical expression evaluation/reduction?

For example if I type '-0.005 + -0.345' how do I get Visual Studio to reduce that (i.e. replace it with the reduction)? Do I have to write a macro? If so, are there any pre-existing macros to do this type of expression reduction?

Just to be clear, I want to be able to highlight an expression and have it replaced with the reduced result. Many are suggesting the immediate window but I fail to see how that will suffice?

Edit I should point out that this is while editing not running or debugging. The immediate window is of little to no use. I also consider this a language neutral question. I would certainly be interested in seeing alternative macros to the one I had posted.

Edit Going Once... Going Twice... (i.e. any other suggestions before I consider accepting my own answer?)

Was it helpful?

Solution

Thank you for the above answers.

There probably are better ways, but here's a quick and dirty macro that does what I need.

References to the System.Data and System.XML namespaces need to be added.

Highlight the expression you want to evaluate and run the macro (it uses the calculated column in the DataTable to evaluate the expression.) It will replace the expression with the reduced result.

Edit - Updated code below. It worked extremely well for reducing a large number of expressions. As pointed out by others there is the immediate window but this will not work for editing purposes. This macro is a language independent solution for basic expressions "(), +, -, *, /".


Sub Eval()
  Dim ts As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
  Using dt As New DataTable()
    dt.Columns.Add("Expression", GetType(Double), ts.Text)
    dt.Rows.Add(dt.NewRow)
    ts.Text = CDbl(dt.Rows(0).Item("Expression"))
  End Using
End Sub

OTHER TIPS

Visual Studio by default will not do any mathematical expression evaluation / reduction. I'm not sure if you can get support for that via items like ReSharper, but if it is available it will be in an add-in.

Also, it would be helpful to know the language you are working in?

Some languages may be helpful in this area. F# for instance makes it easy to evaluate expressions in the IDE via the interactive window and will display out the result. This could easily be added back into your code but it doesn't appear to be exactly what you're looking for.

Here's an answer: Yes, it is possible using the following steps. (While technically performing what you're asking for, I'm not sure it will be extremely useful. :-)

  1. Set a breakpoint in your program that's likely to get hit when you debug the program.
  2. Then, run your program under the Visual Studio debugger.
  3. When the breakpoint is hit, open the Watch window.
  4. In the Watch window, add a new watch by clicking in the Name column.
  5. Enter your expression '-0.005 + -0.345' (without the quotes) then hit [Enter]. ... You should see the Value column get populated with -0.35.

Of course, that isn't in the context of the editor window ... which is, presumably, where you'd want to perform the reduction. So again, not very useful, I imagine. An add-in is the likely way to do that in the editor window.

You could just go to the immediate window and type "?<yourExpression>"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top