Function Evaluation within Immediate Windows with No Side Effect


I make quite heavy use of Immediate Window. It’s a nice tool within Visual Studio to print variable values, evaluate expressions and even execute statements. However, there are times when doing this type of activity changes the state of particular object.

Let me describe the scenario with an example. Let’s say I have an Employee class as follows:

This class is consumed as shown below:

Once the break-point is hit, Salary property of this object can be inspected within Visual Studio.

If I need to run a function of this object, I can easily do it from Immediate Window. In this case, if I call GiveEmployeeARaise method of the Employee object, that function changes the value of Salary which is reflected in Watch window here.

There could be times when I don’t want these test runs of certain function change the state of this object. This is where you can call the method with nse (No Side Effect) switch. Let’s call the same method again but using the switch.

So this time, the Salary property didn’t get incremented.  Isn’t this cool? When I am sure that there is no problem with that state change, you can just remove the nse switch.

Until next, happy debugging.

Filed under: CodeProject, Debugging