Ramakrishna Neela

  • Quick links to download VJ# Express SKU 2005 and J# Redistributables

  • Immediate Window in Visual Studio 2005

    Hi All,

     

    I’m Ramakrishna Neela, working with Microsoft IDC as SDET in VJ# team for past 2 years.

     

    Here I would like explain some basics of “Immediate Window” which is a feature in Visual Studio 2005. This feature allows executing and evaluating code without running the whole program. You can use the VS 2005 “Immediate Window” to execute a function or method while your application is not running. If the function or method contains a breakpoint, Visual Studio will break execution at the appropriate point and allows you to examine the program state. To view the Immediate Window, open/create a project then choose Windows from the Debug menu and select Immediate Window.

     

    To start with, we will create a small program….

     

    Program.jsl

     

    public class Program

    {

          public static void main(String[] args)

          {

          }

         

          public static int Add(int a, int b)

          {

                int temp;

                temp = a + b; // BP here

                System.Diagnostics.Debug.WriteLine(" Sum of is: " + temp);

                return a + b;

          }

          //non static method

          public int AddMethod(int a, int b)

          {

                int temp;

                temp = a + b;

                System.Diagnostics.Debug.WriteLine(" Sum of is: " + temp);

                return a + b;

          }

    }

     

     Without building the application we can test/execute Add method using Immediate Window. To do this, click on Debug à Windows à  Immediate Window and type in following

     

    Add(10,20);

     

    This should result in

     

    Add(10,20);

     Sum of is: 30

    30

     

    This is very quick way to test your method without debugging your entire application. If you put Break Point at some place in above Add method and re-execute above steps then it breaks at the Break Point and allows watching entire context of the program. From then, all debug command like continue, step-in, step-out works as if you’re debugging entire application.

     

    Immediate Window provides few commands to perform few actions

     

    ?<expression>

    This will evaluate the expression and prints the result

    For example, put a break point at some place in above Add method and execute following commands

     

    Add(10,20);

    ?a

    10

    ?b

    20

    ?temp

    0

    ?a+b

    30

     

    Immediate Window also allows you to execute commands prefixed with “>” (greater than sign). Eg

     

    >Debug.Print a  ( this prints the value of at the time execution)

     

    >Debug.CallStack ( this will show call stack)

     

    Immediate Window has IntelliSense support also. There are few shortcut that commands available from IM

    >K - display the callstack.

    >U - display the disassembly of a program.

    >~ - display the status of a thread

     

    Executing non-static methods of a Program

     

    Till now we have seen how to execute static methods, instance method can be invoked by creating instance of type.

     

    Eg.

     

    new Program().AddMethed(10,20);

    Sum of is: 30

    30

     

    This even allows you to create Metadata types like SystemString, System.Object etc of mscorlib.dll

     

    Eg

     

    new System.String("Hello");

    "Hello"

     

    But it will be painful to type entire class name for every method that you want to test. “Object Test Bench”  is another solution to test or debug all your methods of your class. From Immediate Window execute the following for your Program.jsl

     

    Program p = new Program();

    {ConsoleApplication1.Program@25468f2}

     

    This will create an instance of  type Program and places one Object in “Object Test Bench”(OTB) window (see Figure1.0 below) and now you can execute all the methods that are in Program class by right clicking on Object in OTB Window.

     

    Figure 1.0

     

     

     

      

     

    Now one can debug /test any method in your class from OTB just by instantiating that type from Immediate Window.

     

     

     

This Blog

Syndication

Tags

No tags have been created or used yet.

News

Do you have question on VJ#? then come to VJ# Forum

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker