Alex Rivera | Logout

Visual Studio - How to change the return value of a method in the debugger?

Asked 2009-01-19T05:43:38.287
13

When I'm debugging, I often have to deal with methods that does not use an intermediate variable to store the return value :

   private int myMethod_1()
   {
      return 12;
   }

   private int myMethod_2()
   {
      return someCall( someValue );
   }

Well, in order to recreate a bug, I often have to change values on the fly. Here, I could want to see what happens when myMethode_1 return zero. Same thing for myMethod_2.

Is there a way to do that without modifying the code ? What I would like to do, it's to place a breakpoint on the return line (or on the closing bracket) and type-in a new return value.

Note : I'm using Visual Studio 2005 and Visual Studio 2008.

Thank !

Edit
Report

2 Answers

0

Why do you want to change the return value in the called method? You can easily change it on-the-fly in the Calling method. As soon as your function returns, you get the value returned and there you can change it.

   private int myMethod_1()
   {
      return 12;
   }
//call would be something like this
int x = myMethod_1();
//Here you can change the value after the execution of the above line.

Even if you are not storing return value in a variable, and you are directly passing it to some other method, even then you can change the value by stepping into that method and changing the argument value there. Am I missing something here?

answered 2009-01-19T06:01:01.870
-1

I think you can do it in Visual Studio but that feels dirty to me.

My thoughts are you should really be looking into a unit testing framework and mocking so that you can fake the output of myMethod_2 as a 0 and see what effect it has on your code. IMO. This way you can leave the return 0 check in the unit test to make sure the ugly bug doesn't stick its head up again.

answered 2009-01-19T05:54:10.543

Your Answer