The Immediate Window: Working with Members

Tips Search

If you like the tips then you'll love the book!

Order your copy today:
http://www.amazon.com/gp/product/0735649928

The Immediate Window: Working with Members

  • Comments 11

Keyboard:  CTRL + ALT + I
Menu:  Debug -> Windows -> Immediate
Command:  Debug.Immediate
Versions:  2008,2010
Published:  9/28/2010
Code:  vstipTool0095

 

When using with the Immediate Window, you can work with class and object members directly:

 

Debug

You can use any method or property as long as it is in context.  So, for example, when you are in debug mode, you can call any method that is in scope:

image

 

 

Design

A less-known feature is you can work with properties and methods while in design mode.  If you have static methods on a class, for example, you can just execute them right away:

image

 

 

For object members, obviously, you need to create an instance of the object before working with the members:

image

 

WARNING:  When working with members at design-time a build will occur.  This could have unintended consequences so make sure you have experimented with this feature a bit before you use it.

  • What can I do in the immediate window when I am in native C/C++ code?

  • Design time debugging doesn't work for me.  I have a public static method (all it does is writes "test" to the output window), and I get the following message:

    The expression cannot be evaluated while in design mode.

    Am I missing a studio setting somewhere to enable this?

  • @Daniel -- I haven't really dug into the C++ stuff yet but am thinking I wil include some in the book if I have time before publication.  

    @Aaron -- you should be able to execute directly in VB and C#.  Can you show me what the code looks like?

  • @Aaron, @Zeinab - same thing for me. I have productivity tools installed, added a simple class with the static method, and tried to call it while in design mode. The result - like in @Aaron case :(

    Any help? Thanks!

  • Weird.   Let me poke around and see if this is a known issue.

  • Zain, if you figure it out, could you just write a new post on it?  I think it's a fantastic feature that many folks would benefit from.

    Thanks for all of you contributions, you rock!

  • Same problem as Aaron...

    I'm running Visual Studio 2010, and have a simple asp.net app.  I've created a class file (VB) with a public method.  When I type in the Immediate window, the intellisense works and finds class_name.function_name(), and it responds "The expression cannot be evaluated while in design mode."  Trying to run in Debug mode says "The expression cannot be evaluated while in run mode."

    Thanks!

  • I'm having the same problem as Geoff and Aaron.  I have a native C++ DLL built in VS 2010.  I tried executing an exported static function or even a simple expression like 2+3, and I get "The expression cannot be evaluated while in design mode."  I also tried running another project in the same solution as the DLL in Debg mode, but I got, "The expression cannot be evaluated while in run mode."

  • @Aron, @ms440m, @Geoff:

    Not quite sure if this can be helpful:

    msdn.microsoft.com/.../f177hahy(v=vs.80).aspx

    Design Time Expression Evaluation in Multi-Project Solutions

    When establishing the context for design time expression evaluation, Visual Studio references the currently selected project in Solution Explorer. If no project is selected in Solution Explorer, Visual Studio attempts to evaluate the function against the startup project. If the function cannot be evaluated in the current context, you will receive an error message. If you are attempting to evaluate a function in a project that is not the startup project for the solution and you receive an error, try selecting the project in Solution Explorer and attempt the evaluation again.

    Keep in mind that if you are working with Web Applications you need to debug your app and hit a breakpoint (just place a breakpoint on your start page of the project) before evaluating from the immediate window. Otherwise you will be greeted with " The expression cannot be evaluated while in design mode." or "The expression cannot be evaluated while in run mode. "

    Hope it helps.

  • Hey All -- just retested this with shared/static methods in VB/C# and it does work for those scenarios.  It does not work in C++ as near as I can tell.  The C# example is just like the one in the post.  The VB example class looks like this:

    Class Jack

           Shared Function DoSomething()

               Return 10

           End Function

    End Class

    And this is what I put into the Immediate Window from Design Mode(with the result shown as well):

    ?Jack.DoSomething()

    10 {Integer}

       Integer: 10

  • When in design mode, just rebuild your solution, and you can test anything from your solution in the immediate window.

    As simple as that.

Page 1 of 1 (11 items)
Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post