Jason Irwin
Jason Irwin

Reputation: 2045

Visual Studio Debugger - Automatic Variable Assignment

I am working on a multi-developer project and the application being developed is launcher through a launcher application which passes parameters such as the user logged in, their location, etc. Right now when I debug the application I set a breakpoint on the code that parses the input parameters and I assign the username variable my username etc.

I could hard-code these values but:

My question is:

Thanks in advance

Upvotes: 6

Views: 384

Answers (4)

Protector one
Protector one

Reputation: 7291

You can set variables using Trace Points.

Set a Breakpoint where you wan't to set the variable. Right click the red glyph and choose actions (from keyboard, you can sort of do it with alt+F9, c and some Tabs and spacebar action). As the message to show, you can input a string with expressions to be executed in curly braces.

E.g.:

2 + 2 = {2 + 2}

which will output "2 + 2 = 4". In an expression, you can also assign variables:

{myVar = 5}

This does have the unfortunate side effect of printing the result value of the expression to the Output Window.
To circumvent this, you could assign the variable with a Void returning helper extension method:

public static class HelperFunctions
{
    public static void AssignTo<T>(this T value, out T variable)
    {
        variable = value;
    }
}

Use this like:

{"new value".AssignTo(out def)}

This will only output a newline to the Ouput Window (which might be less bad or worse, depending on your preference).

To assign a variable without polluting the Output Window, it would be great if you could assign a variable in the Trace Point's Conditions, but no matter how many helper functions I try to concoct, that doesn't seem possible. I appears as if variables in the Condition expression are sandboxed, whereas those in the Action message expression are not.
 

 

Edit

Assigning variables in the Breakpoint condition was possible in Visual Studio 2013, but in 2019 side effects (like variable assignment) are discarded: https://developercommunity.visualstudio.com/content/problem/715716/variables-cannot-be-assigned-in-breakpoint-conditi.html

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942257

Strange question. It was posted a dog-life ago but the concepts of unit testing and test-driven development were already well established back in 2009. With excellent tooling available, like nunit and testdriven.net. A debugger is not the right tool to use, that just produces gray hair.

Upvotes: 0

shahkalpesh
shahkalpesh

Reputation: 33474

Adding to JaredPar's reply, make the class partial.

This will let you keep the main file (the one that gets checked in) updated. The other file (the one containing IF DEBUG) will be local & won't get checked in.

Hope that helps.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755357

When I hit situations like this, I usually a combination of conditional compilation and environment variable / reg key. You can use these storage mechanisms to host your information without hard coding it into the application.

#if DEBUG
if ( null != Environment.GetVariable("CUSTOM_INFO")) {
  userName = Environment.GetVariable("CUSTOM_USERNAME");
  ...
}
#endif

This way it won't affect any other developers. Unless of course, they want to accomplish the same thing.

Upvotes: 1

Related Questions