Henrik Berg
Henrik Berg

Reputation: 549

Create initializer during debugging (e.g. in the immediate window)

Is there any way to output a value in Visual Studio in a format that can be used directly in code to initialize a variable?

For example, let's say I have an array called anArray, and, during debugging, it has been given some content. Now, in the immediate window, I can easily print the content, which will be e.g.:

{double[3, 3]}
    [0, 0]: 1.0
    [0, 1]: 2.0
    [0, 2]: 3.0
    [1, 0]: 4.0
    [1, 1]: 5.0
    [1, 2]: 6.0
    [2, 0]: 7.0
    [2, 1]: 8.0
    [2, 2]: 9.0

Now what I'd like to have is the same information, but printed in a format that allows it to be pasted into code, that is, something like this:

new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }

Is this possible?

Similarly, it would be nice to do the same with other kinds of (simple) objects, like this class:

class TheClass
{
    public int TheIntProperty { get; set; }
    public double TheDoubleProperty { get; set; }
}

Creating and printing an object of the class gives the following output in the immediate window:

anObject
{ConsoleApplication.TheClass}
    TheDoubleProperty: 0.5
    TheIntProperty: 2

while what I would like to have is this:

new TheClass
{
    TheIntProperty = 2,
    TheDoubleProperty = 0.5
};

I suppose I could make a simple script to achieve this, but figured this might be a feature that was already present in Visual Studio. So, is it?

Upvotes: 1

Views: 225

Answers (1)

Henrik Berg
Henrik Berg

Reputation: 549

Just found a solution myself. A little third-party tool called Object Exporter seems to do what I need:

http://www.omarelabd.net/exporting-objects-from-the-visual-studio-debugger/

Doesn't work on multidimensional arrays, though. But other than that, seems ok.

Upvotes: 2

Related Questions