Link to home
Create AccountLog in
Avatar of Sinclair
Sinclair

asked on

"Step Back" in a Visual Studio debugger

Hi all,

I'm using Visual Studio as my C# IDE. In Visual Studio, is it possible to debug variable values in environment frames (on the stack) that belong to the calling method ? I.e., let's say I have this code:

private void DoLoop(int n) {
  for(int i=0; i<n; i++) {
    Thing t = myThings[i];
    DoSomething(t);
  }
}

private void DoSomething(Thing t) {
  t.CallSomeMethod();
}

If the DoSomething function throws a Null Reference Exception because "t" is null, is there a way to tell what value "i" had in the DoLoop method ? I know that I can just insert a null check into the DoSomething method, but I'm looking for a more general solution; the above is just a simplified example.
Avatar of Gautham Janardhan
Gautham Janardhan

is it through code or thru the debugger

u could try this in code

private void DoLoop(int n) {
  for(int i=0; i<n; i++) {
   try
{
    Thing t = myThings[i];
    DoSomething(t);
}
catch(Exception ex)
{
  throw new Exception(ex.message +":Value of i is" +i.ToString();
}
  }
}

private void DoSomething(Thing t) {
  t.CallSomeMethod();
}
Avatar of Sinclair

ASKER

Yeah, I understand that I can alter my code, but what I'd really want is to examine the stack frame in the debugger. It's not always as easy as getting an exception; sometimes, you just get a wrong answer, and you'd like to know why.
ASKER CERTIFIED SOLUTION
Avatar of sumix
sumix

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks sumix, that worked.