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

前端 未结 6 450
轻奢々
轻奢々 2021-01-02 08:12

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


        
相关标签:
6条回答
  • 2021-01-02 08:37

    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.

    0 讨论(0)
  • 2021-01-02 08:44

    For those who are looking for a solution to this in VB.NET:

    It was so simple, I can't believe I did not see it : To look at the value a function will return : just place the pointer over the function's name. The value will be shown in a tool tip.

    The change the value : just click on this tool tip, change the value and hit enter.

    Visual Studio is very cool !

    Note : I tested it in VB.NET on Visual Studio Team System 2008. Just tried using C#, but it does not work... :-(

    0 讨论(0)
  • 2021-01-02 08:44

    What you need is Mocking. Checkout mocking frameworks like Rhinomock, Moq or Typemock.

    For the kind of scenario you are describing, I am going to assume that you may not have clean contract - driven development and may need to return in fake behaviour/state. In this case, Typemock may be the best option for you although it is commercial

    0 讨论(0)
  • 2021-01-02 08:49

    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?

    0 讨论(0)
  • 2021-01-02 08:53

    Don't think so.
    You'd need to have a temp var in the callee or the caller so as to get a handle on the value to modify it in the IDE Debugger/QuickWatch window. So the simplest and fastest way to do it would be to comment existing code and make a temporary change for debug-what-if.

    either

    private int myMethod_1()
    {
      var x = 12;
      return x;
    }
    

    Or

    private int myMethod_2()
    {
      var y = someCall( someValue);
      return y;
    }
    

    Don't see it worth the aggravation of 'avoiding a code change' to do this. If I FUBAR, I do a checkout and we're gold again.

    0 讨论(0)
  • 2021-01-02 09:00

    Return values from functions are usually returned in the EAX register.

    If you set a breakpoint just at the end of the function then there's a chance that changing EAX would change the return value. You can change and view any register in visual studio simply by writing its name in the watch window.
    This is likely to fail if you have optimization on or even if the function is something simple like return 12. it's probably also not going to work if you're returning something that doesn't fit in a 32 bit register. In the least it's worth trying.

    0 讨论(0)
提交回复
热议问题