views:

216

answers:

5

This could save me so much time. Sometimes I find myself writing things like this in the watch or immediate window:

MyObject.Function1.Fuction2.Fuction3.Fuction2

Instead I could just declare several new variables and do this in a more structured way.

However doing this is not allowed.

Is there some way that i can do this? Is there going to be support for what I want in any future versions?

+2  A: 

@AMissico has pointed out my earlier mistake - apparently you can do this in VB.NET with an implicit assignment.

I'd still consider this a crutch, and if I ever found myself needing to do it, I'd start asking why the program is structured in such a way that makes it necessary.

If you need this level of debug information, it is a better practice to put it in your program logic instead - that way, when future maintenance programmers have to debug the same code path, they'll have all of the same detailed information without having to "interrogate" deeply-nested object graphs. Instead of writing MyObject.Function1().Function2().Function3(), write:

var first = MyObject.Function1();
var second = first.Function2();
var third = second.Function3();

Then you can step through the actual program logic in a debugger instead of writing an entire test script in the Immediate window.

You might also consider writing your own Debugger Visualizer if you need to get a detailed representation of a complex object or hierarchy.

Aaronaught
You don't understand, I am debugging hierarchies. Thats how they are designed. I need to declare some vars in order to debug them efficiently.
diamandiev
@diamandiev: So declare the variables **in the code**, not in the debugger. Or create a visualizer, which doesn't require changing any code at all. Is there something preventing you from doing this?
Aaronaught
@diamandiev: create variables in code, so you can use them in the Immediate Windows. Put the variables in a Module with Public scope. Also, since you are debugging, create test procedures in the module, so you can call them from the Immediate Windows and set breakpoints in the test procedures.
AMissico
-1, see my answer.
AMissico
"I'd still consider this a crutch" Yes, I agree. In all the years I been programming, I have only need to do this few times. If I could not figure out quickly, I would create test methods and procedures where needed then delete them once done with troubleshooting.
AMissico
@diamandiev: You can add these variables and method calls to the Watch window to do what you want.
AMissico
A: 

This might be an alternative: You can use the Object Test Bench to create an instance of MyObject, and invoke a method of the instance with whatever arguments you need (or just invoke the method without creating an instance, if it is a static method).

M.A. Hanin
Doesn't work for VB, unless you know of way.
AMissico
I'm using it all the time (in VB). It can only be used on the active project in the solution. You create an instance (or invoke a static method) using the Class View window, and manipulate instance methods using the Object Test Bench window itself.
M.A. Hanin
Oh, that is right! Only the active project. I always forget that. Just like I forget I can change the active project to get to the object in question.
AMissico
+1 (but it won't let me vote because vote is too old, which is weird, since I haven't voted.)
AMissico
I now have a post-it on my monitor to remember the Object Test Bench! Thank you very much M.A. Hanin. YOU ARE AWESOME!
AMissico
@AMissico, you're welcome :-)
M.A. Hanin
A: 

In addition to what Aaronaught says, Such constructs as you describe violate the Law of Demeter . You may want to consider restructuring your program so that such constructs are unnecessary. In fact, even the solution offered maintains the violation of proper separation of concerns.

If a method references no more than one level of indirection, then the behavior of the debugger will be more appropriate to the application.

Jeffrey L Whitledge
A: 

With C# you can declare variables in the immediate Window during debugging (I honestly don't know if it is a VS2008 feature only, but I have just verified it in VS2008 team edition).

You can't declare variable in the Watch window (in fact the error message says "Declaration statements are only allowed in the immediate window") but you may watch any variables you have created in the immediate window.

Also, you can't use var when declaring variables in the immediate window, but apart from that you can do what you're asking.

Brian Rasmussen
i don't use c# too bad. why does c# always get all the cool stuff?
diamandiev
When down voting please leave a comment.
Brian Rasmussen
I voted -1 because you can. Just can't use `Dim` or `As ...`.
AMissico
@AMissico: I wasn't really claiming anything. As I said I am not a VB programmer, but the error message indicated that it wasn't possible. The OP did not specify which language, so the link could offer some explanation. I'll remove the mention of VB.
Brian Rasmussen
@Brian Rasmussen: I understand. That is probably the most misleading error message. Oops, my grammar in comment is completely wrong. (It was late.) Should be "...because you can set a variable."
AMissico
+3  A: 

Do not use Dim

jack = 12
? jack
12 {Integer}
AMissico
I find it slightly disturbing that you're allowed to make implicit assignments like that... but a right answer is a right answer, +1.
Aaronaught
The issue is wrong question.
AMissico