Executing Code Snippets From IDE at Design Time
- 08 Apr 2006 15:44
You surely know what I'm talking about. We often need to run and test simple code chunks or only one statement while we are coding. It may be very annoying to run main project every time we want to test our new five lines long function. Fortunately, there are better ways to do it.
Here are all options that I have found:
- As already mentioned, place the code snippet to your main project. For example, you can insert it in some button event handler and then test it by pressing the button. This is cumbersome and time consuming.
Don't place it in your main code. Just run the main code in debug mode and pause it, e.g. with breakpoint. Then open immediate window and test your expression. This has one big disadvantage, you cannot declare new variables or functions in immediate window.
The two previously mentioned methods have one disadvantage. You cannot do it at design time, you need to run your project. (UPDATE: You can use Immediate window in design time in VS 2005 and higher. In VS .NET 2002/2003 you get "The expression cannot be evaluated while in design mode." error.) Or you need to have opened dummy project in separate VS instance. The following two solve this problem.
- Use Macro IDE. This is my favourite approach but it only works for VB .NET. As you already know, VS macros are nothing else than VB. NET functions So, just create macro with name "Test" and insert your code there. Then you can run it or debug it.
- Use Snippet Compiler. It compiles code snippets and works with VB .NET and C#. While it's not VS add-in but standalone application, you can still call it from VS. Just define it as external tool in Tools - External Tools. You can also place it on the toolbar or menu or even right-click context menu the same way you do it with built-in commands and macros.