views:

248

answers:

4

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?)

A: 

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.

JaredPar
This project is VB.NET.
@neodynium, then no there are no expression reduction options from the IDE at this point.
JaredPar
A: 

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.

Chris W. Rea
p.s. The Immediate window would also evaluate the expression, but again, not that useful for editing.
Chris W. Rea
+1  A: 

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
A: 

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

RBarryYoung
The immediate window will not work for editing purposes.
Why not? I use it all the time while editing.
RBarryYoung
@RBarryYoung - I want to be able to highlight and expression and have it replaced with the reduced result. How do you achieve this with the immediate window?
You can't but then you didn't mention that requirement originally, that's why I asked. Without that condition the immediate window works just fine, you can cut and paste to it, execute, and cut and paste the answer back if you want. It did not always work while editing in early versions of VS.Net, but they re-added this feature some time ago (VB6, and all earlier versions of VB, QB, and all other BASICs since forever have always had this feature).
RBarryYoung
I did mention it "(i.e. replace with reduction)" although it may not have been the clearest explanation. I agree that one could copy and paste with the immediate window for a limited number of expressions, but that would be awefully ineffecient for a bunch of expressions.