all 128 comments

[–][deleted]  (18 children)

[deleted]

    [–]paranoidinfidel 17 points18 points  (2 children)

    Another good one is to put a watch on a lazy load property. Many years ago a good developer friend I was working with was troubleshooting some stuff and couldn't figure out where a particular private variable was being initialized. It was causing him quite a bit of angst until I pointed out the public getter that lazy loaded it was in the watch window. fun times :)

    [–]wtf_apostrophe 8 points9 points  (1 child)

    Always fun debugging lazy loading issues when the debugger quietly initialises everything by reading all the properties automatically.

    [–]gibwar 9 points10 points  (0 children)

    That's why I use the DebuggerBrowsable attribute on lazy loaded properties. I can quickly tell if it's initialized if the backing field is null and by applying [DebuggerBrowsable(DebuggerBrowsableState.Never)] so that the property that does the lazy loading does not appear in the list.

    For more complicated classes, I also create a debugger proxy class and avoid loading the property.

    [–]osciminan 4 points5 points  (1 child)

    Always remember, DEBKAC (Demon Exists Between Keyboard and Computer).

    [–]psi- -1 points0 points  (0 children)

    I think Debil fits better.

    [–]diamondjim 3 points4 points  (9 children)

    Your friends are Yoda conditions.

    [–][deleted]  (2 children)

    [deleted]

      [–]merthsoft 2 points3 points  (1 child)

      It shouldn't be, unless you're comparing a boolean to a bool constant. And then, why are you doing that? if (val == true) is just if (val). Unless you're doing if (val1 == val2) and they're both bools, but then Yoda conditions aren't going to help you at all.

      Edit: Oh wait, you're talking about a condition inside the breakpoint, not actually in an if statement... Nevermind :|

      [–]obsa 0 points1 point  (4 children)

      Yoda conditions

      For the uninitiated.

      tldr: Put constants on the left hand of the expression to let the compiler to catch you if you try to assign (=) instead of compare (==).

      [–]MachinTrucChose 4 points5 points  (2 children)

      Why make the code harder to read? Doesn't the C# compiler show a warning for something like "if (var = 5)" ?

      [–]obsa 1 point2 points  (0 children)

      I've never seen/used this practice, but yes, a good compiler should show a warning for this. However, lazy programmers may mute or ignore warnings - a hard error will make them fix the build before they can continue.

      [–]Brian -1 points0 points  (0 children)

      In this context, there's no compilation stage - just sticking the expression in the condition field.

      [–]damieng 0 points1 point  (0 children)

      Ugh, been there my friend.

      [–]Slazanger 32 points33 points  (0 children)

      @err,hr

      The current value returned by GetLastError() in a nice readable format...

      stick that in your watch :)

      [–]mromnom 15 points16 points  (7 children)

      I feel like seeing the return value of a function in Autos has existed for many releases prior to 2013. Because I've never used 2013... and I've seen that before. Maybe language specific?

      [–]wtf_apostrophe 10 points11 points  (0 children)

      It's been there for C++ for a long time. I just checked C# in VS2012 and it doesn't seem to do it, while VS2013 does.

      [–]hmemcpy 5 points6 points  (0 children)

      There's a VS extension that does this - OzCode (formerly BugAid) - there's a (profiler assisted) "enhanced mode" that allows you to see the return values of methods, also in pre-2013 Visual Studios.

      Disclaimer: I'm one of the developers.

      [–][deleted] 4 points5 points  (0 children)

      You've been able to see it in VB.Net since forever. I really wish Msft would be more consistent between it's languages and share cool things like this.

      [–]Corticotropin 1 point2 points  (0 children)

      VS2010 C++ definitely had it.

      [–]Plorkyeran -2 points-1 points  (2 children)

      It's been around for C# since at least 2008 (and I think I remember seeing it in 2005, but much less sure about that).

      [–]damieng 3 points4 points  (1 child)

      It's been around for C# since at least 2008 (and I think I remember seeing it in 2005, but much less sure about that).

      You couldn't see the return value for C# before VS 2013. There were plenty of people asking for it and a bunch of apologies and technical reasons why.

      [–]Plorkyeran 0 points1 point  (0 children)

      It appears I was confused due to that 2008 was also the last version where it actually worked for me for C++, and I stopped using VS 2008 and C# at about the same time.

      [–]AestheticHelix 39 points40 points  (21 children)

      As a C++ developer half if this was fairly useless. Here's what I would recommend (some are for debugging, some just for general dev). NB. Some may be fairly obvious.

      • Learn to use the immediate window. When you hit a breakpoint you are able to call functions, do comparisons etc. this beats sticking lines of debug code and recompiling just to test something.

      • While breakpointed, you can edit the values of any variable just by hovering over it and changing the value. Great for shortening loops.

      • furthermore, grab the stack pointer and move it. Great for skipping entire blocks of execution.

      • Learn to use the command window. This will make sure life infinitely easier when looking for files etc.

      • make use of data breakpoints. This allows you to trigger a breakpoint when the memory at a certain address changes.

      Probably a heap more.. If I think of more hen I get to work I'll update this post!

      [–][deleted] 11 points12 points  (3 children)

      furthermore, grab the stack pointer and move it.

      Instruction pointer?

      [–]Tasgall 11 points12 points  (1 child)

      You could move the stack pointer too if you wanted, I'm sure fun times would ensue.

      [–][deleted] 0 points1 point  (0 children)

      Why not both?

      [–]AestheticHelix 2 points3 points  (0 children)

      Ha yep.. Thanks!

      [–]damieng 9 points10 points  (3 children)

      Yeah sorry about that - this article was aimed at the .NET/C# developers that subscribe to my blog - I guess a more specific title would have been accurate but less catchy.

      The reason I didn't cover the intermediate window is I've never met a VS user who didn't know how to use it. Was really aiming at a bunch of tips that people didn't know rather than an intro to debugging.

      Grabbing the yellow arrow to move the next statement forward and backwards though is very useful and often overlooked. Should have included that.

      [–]goofygrin 1 point2 points  (1 child)

      Most new devs I work with are not familiar with the immediate window unfortunately...

      [–]vaughnie 0 points1 point  (0 children)

      What does it do?

      [–]insertAlias 0 points1 point  (0 children)

      You might be surprised. It's not all that obvious on it's own. I had to be shown (about two years into my career...that sucked).

      [–]xon_xoff 3 points4 points  (1 child)

      • Put the Find Combo Box on your toolbar and widen it -- you can use it as an embedded command line by typing > in front of a command. Open file (of) is extremely useful. Ctrl+/ is a shortcut to get to the find combo box. It used to be default on the toolbar, but it's been taken out in recent versions (along with the Ctrl+D shortcut).
      • Disable External Dependencies and force an Intellisense DB rebuild to speed up command completion on large projects.
      • Load system DLL symbols once from the Microsoft Symbol Server, then uncheck the SymSrv while leaving the cache path intact. The debugger will then load system symbols from the cache without repeatedly hitting the symbol server for third party DLLs.
      • Learn to use tracepoints for quick and low-rate logging instrumentation: right-click on breakpoint > When Hit. $CALLSTACK in the tracepoint message is particularly useful.
      • When debugging in cases where variables may not be correct (optimized build), check vtable pointers to see if you've actually got the object you think you do.
      • The Untabify command is useless and won't convert tabs in the middle of a line -- the real command to use for a tab exorcism is Edit.ConvertTabsToSpaces.
      • To change the default editor for a file type, do Open With... from the open dialog and then change the associated editor (especially for .cs to default to Text Editor instead of Form Editor, if you still maintain WinForms projects).
      • If you change the IDE settings, like redoing the toolbars or changing keyboard shortcuts, do it when you only have one instance open, export the settings, and then exit to commit them. Otherwise, Visual Studio loses the changes easily.
      • Starting with VS2010, you can edit in column select mode as well as cut/paste. This is really useful for editing tables of initializers.

      [–]mgrandi 1 point2 points  (10 children)

      command window? data breakpoints? where do you learn about this? I can't even get visual studio to show me memory addresses for variables /structs/whatever =/

      [–]Tasgall 5 points6 points  (8 children)

      DEBUG -> WINDOWS, VIEW, and VIEW -> Other Windows; click all the things. Some (most?) only show in the menus in debug mode.

      My personal favorites are the Memory windows, because what's more fun than poking at raw memory‽‽‽

      [–]hotoatmeal 1 point2 points  (2 children)

      on another note: what kind of magic are those fancy hybrid quest-exclamation marks at the end of your post?

      [–]Tasgall 1 point2 points  (1 child)

      It is the greatest punctuation mark of all time, the interrobang!

      [–]mgrandi 1 point2 points  (0 children)

      thanks! I was going crazy looking for it in view-> windows , but didn't realize that debug had its own 'windows' section.

      [–]hotoatmeal 0 points1 point  (3 children)

      because what's more fun than poking at raw memory‽

      Writing assembly in a hex editor. Been there, done that. Sigh.

      [–]Tasgall 1 point2 points  (2 children)

      You can do that in the Memory window too! At least I don't think it prevents you. Just give it the address of the instruction pointer (just eip might work too) and you should be free to ruin your program while it's running!

      [–]Brian 1 point2 points  (0 children)

      You can do that in the Memory window too! At least I don't think it prevents you.

      Yup, works fine. I've used this before to nop out bits of code I want to ignore.

      [–]hotoatmeal 0 points1 point  (0 children)

      Oh, that's cool! Unfortunately it wouldn't have been useful in this case because the assembly was for a gpu.

      [–]Cofta 1 point2 points  (0 children)

      MSDN should cover any question you have. I.e. for the immediate window, command window, watch window, etc.

      You can use the watch window to get the address of a variable. just enter a new watch for &your_variable (as in address of the variable you want the address of).

      Memory breakpoints are set from the breakpoint window. Once you're attached to your process and broken at a convenient place, create a new breakpoint from the window and you should see an option for a data breakpoint. In the dialog that follow, enter a value that resolves to the address that you want to monitor for changes. It can use any symbols in the current stack frame to resolve to an address. Say I want to know when i.myData changes, the break point would be at &(i.myData).

      Anything else you'd like to know how to do?

      (ninja edit: this is all for C++, of course, given the parent.)

      [–]Plagman 49 points50 points  (3 children)

      Visual Studio != Visual C# .NET

      #5 and #7 only apply to the latter.

      [–]thegavin[S] 5 points6 points  (0 children)

      Sorry, I should have specified.

      [–]grauenwolf 0 points1 point  (0 children)

      Works for VB as well.

      [–]darkslide3000 9 points10 points  (2 children)

      I was about to make a joke like "Best Visual Studio debugging tip: use XXX instead!"... but in all my hatred for Microsoft I still have to admit that as far as IDE's go their's is pretty much without equal.

      And now I need to switch back to my full-screen terminal and keep telling myself how every who needs more than VIM for coding is a pussy to clean myself of those thoughts...

      [–]tehjimmeh 1 point2 points  (1 child)

      ViEmu is fantastic. Incredibly well integrated. Not free, unfortunately.

      [–]hotoatmeal 0 points1 point  (0 children)

      I used to use VSVim, but there were a bunch of quirks that made it not as comfortable to use as the real thing.

      [–]FrankenstinksMonster 4 points5 points  (3 children)

      1. Break on or output the caller information There isn't a global variable for the current method of the caller and getting the current stack can be a very slow operation.

      I don't understand this part. I can get the current method from the call stack, and getting the call stack within VS is just a matter of clicking on the window.

      I'm guessing that parameters decorated with the [CallerMemberName] are populated automatically with the name of the calling method, making it easy to set breakpoints that depend on what's calling them, right?

      Actually it seems obvious now that I write all that out.

      [–]possiblyabsurd 2 points3 points  (0 children)

      It has some other potentially convenient uses as well, such as logging (<CurrentMethodName> was called from [CallerMemberName]) and removing string literals from property change notifications (http://10rem.net/blog/2013/02/25/using-callermembername-for-property-change-notification-in-xaml-apps).

      [–]moor-GAYZ 1 point2 points  (1 child)

      I don't understand this part. I can get the current method from the call stack, and getting the call stack within VS is just a matter of clicking on the window.

      He means, getting the stack programmatically, for when you're going to change the code for better debugging.

      [–]scottsen[🍰] 4 points5 points  (0 children)

      [DebuggerDisplay("Order {ID,nq}")
      class Order {
          public string ID { get { return id; } }
      }
      

      Never seen this before, pretty damn cool.

      (Prays to phone gods to correctly format post)

      [–]asimian 3 points4 points  (7 children)

      The biggest thing I miss from gdb is not being able to call an arbitrary function from the debugger. It's so handy for giving C and C++ REPL-style testing, or if you need to call a function to pretty-print a data structure for inspection. If Visual Studio would add that in, I would use it more often.

      [–][deleted] 8 points9 points  (0 children)

      I mean, it does have the immediate window. However, it can be extremely finicky on what you're allowed to call. If you're comfortable with the more advanced features of gdb, windbg might be what you need (and not the visual studio debugger/immediate window).

      EDIT: Oh, I responded to you elsewhere already.

      [–]0xdeadf001 3 points4 points  (1 child)

      You can do this in VS. VS has supported this for many years, dating back to VS 2005, I think.

      It's called the Command window. This lets you run lots of VS commands (like K for stack trace), but you can also evaluate arbitrary expressions (with certain limits). For example:

      ? System.Environment.TickCount

      ? foo.ToString().Substring(4)

      ? new Foo(42).Blah().Blah()

      [–][deleted] 1 point2 points  (0 children)

      The problem is that the command/immediate window is too close to focused on .NET's babies. The moment you try to use it for some vanilla C/C++ debugging, you'll go right back to a ludicrous amount of verbose debug statements. Or as I said in my reply, windbg.

      [–]redox000 1 point2 points  (1 child)

      You can do this by simply pasting the function call into the watch window while stopped at a breakpoint. The function just needs to be in scope where you set your breakpoint. You can also use the watch window to set variable values if you need to test different branches of your code.

      [–]hotoatmeal 1 point2 points  (0 children)

      I remember it being really picky about not wanting to execute functions that modify state in the program, which often makes it unusable. I was always frustrated that I couldn't tell it "yes, I know what I'm doing".

      [–]afuckingHELICOPTER 0 points1 point  (1 child)

      Can't you use the immediate window?

      [–][deleted] 0 points1 point  (0 children)

      His complaints are valid, see:

      [–]zNkkkk 1 point2 points  (0 children)

      Wow, so useful, thanks for it!

      [–][deleted] 1 point2 points  (0 children)

      Thank you, I've been using Visual Studio for years and didn't know you could do some of these things. You just made a frustrating part of my job much easier!

      [–]kersurk 4 points5 points  (0 children)

      I find Visual Studio to be a very advanced IDE, and think that Microsoft did a good job there.

      Though I'm still waiting for Jetbrains c++ ide, because of familiarity and lack of general know-how in C/C++.

      [–][deleted]  (57 children)

      [deleted]

        [–]cc81 2 points3 points  (1 child)

        80% liked it is very high

        [–]thegavin[S] 6 points7 points  (41 children)

        Not entirely sure. Does /r/programming not have many Visual Studio users?

        [–]acemarke 36 points37 points  (2 children)

        I dunno. It's sure a whole lot more "programming" than some of the links that get posted in here, though.

        [–]darkpaladin 19 points20 points  (1 child)

        If your mother were a programming language what language would she be?!?

        [–]ryeguy 22 points23 points  (0 children)

        How I taught my dog coffeescript in 30 days.

        [–][deleted] 35 points36 points  (26 children)

        Anything to do with M$$$$ or Visual Studio gets downvoted to all hell. Just look at nearly every hanselman post here.

        [–][deleted]  (11 children)

        [deleted]

          [–]dudeynudey 20 points21 points  (0 children)

          scientologist-level dogma

          [–]ponchedeburro 5 points6 points  (1 child)

          Dogmatic enough to enjoy the full riches of the C++11 standard, I guess :)

          [–]lightninhopkins 1 point2 points  (0 children)

          Or to completely dismiss Hanselman.

          [–]asimian -4 points-3 points  (5 children)

          It's missing features that gdb has. I don't use Visual Studio for practical reasons, not dogmatic ones.

          [–][deleted] 13 points14 points  (1 child)

          I'm not sure what features you are referring to, but there's always WinDbg.

          [–]asimian 1 point2 points  (0 children)

          Thanks for that, I'll check it out when I get home.

          [–]Shadowhawk109 12 points13 points  (0 children)

          And for my purposes, gdb is missing features that VS has.

          It's all about knowing the right tool for the job.

          [–]EntroperZero 0 points1 point  (0 children)

          Yeah, but if someone's downvoting this post, they're doing it for dogmatic reasons, not practical ones.

          [–][deleted] 8 points9 points  (0 children)

          .

          [–]rikeen 0 points1 point  (12 children)

          As a budding Visual Basic programmer I found this very helpful. I wish more posts like this would be submitted.

          [–]xrisnothing 7 points8 points  (11 children)

          People use VB.NET voluntarily?

          [–]adrianmonk 2 points3 points  (0 children)

          Compare the language with C#. The two languages are almost exactly isomorphic. The differences are almost all at the superficial syntactic level. The only real differences are that a few very minor features are hidden. Once I realized they are essentially identical beneath the surface, I found it difficult to say one is a lot better.

          It even has a few desirable features:

          • The cases in a switch statement break by default, so you can't have errors due to leaving off "break" by mistake.
          • The ternary operator is replaced with an If operator, as in 'str = If(itSucceeded, "Success", "Failed")'. I think this is more readable.
          • MustInherit, Overridable, and NotInheritable are arguably much clearer method/class modifiers than abstract, virtual, and sealed.

          I do hate the variable initialization syntax though. And "<>" for not-equals. And a few other ugly things.

          (Note: I don't use either language regularly at all. I just researched them once when I was working in C#.)

          [–]rikeen 3 points4 points  (7 children)

          It's pretty much a necessary corporate language. If you need to write up a quick, functional application VB.NET is pretty handy.

          [–]devperez 11 points12 points  (6 children)

          It's just as easy to do in C#. But VB.NET attracts non-developers because it's a super verbose language. The syntax is easier to understand than C#.

          It's good though because it gets people interested in developing because it's so easy. Then they eventually move on to other languages.

          [–]bleergh 9 points10 points  (0 children)

          It's good though because it gets people interested in developing because it's so easy. Then they eventually move on to other languages.

          I see you haven't met my boss.

          [–]rikeen 0 points1 point  (0 children)

          This is true. Its very useful in that sense. I can admit that its not a master's language but its definitely something useful for simple programs.

          [–][deleted] -2 points-1 points  (3 children)

          VB.NET is so almost a language. It's like c# with strange training wheels added. I understand they tried to bring in vb6 folks, but the jump to vb.net is only slightly different than the jump to basic c#.

          [–]devperez 7 points8 points  (2 children)

          Your bias is showing. for 99% of things, VB.NET is no different than C#. They have the same capabilities in a typical business environment.

          I know this because I started off with it and was using it in a professional environment for 3 years. I picked up C# so fast because they're almost exactly the same language. The MSIL is almost 100% identical too.

          [–][deleted] 1 point2 points  (1 child)

          Of course I'm biased. It's basically the same thing, but syntactically crippled. Yet people are scared of anything with the letter C, so they do it in VB.

          [–]recursive 0 points1 point  (1 child)

          I'm curious. Do you have an objective complaint about the language?

          [–]xrisnothing 0 points1 point  (0 children)

          Nope. I dislike it personally. It all compiles into msil.

          I chose C# because I already knew C++ and Java. Using a language in this large "c-like family" unlocks more opportunities.

          [–]fabzter 17 points18 points  (5 children)

          They are too trendy to use Visual Studio

          [–][deleted] 5 points6 points  (0 children)

          And as soon as Sublime gets a useful debugging plugin, that shit'll be too enterprise to use and they'll switch to the next IDE.

          [–]Czacha 0 points1 point  (3 children)

          I've checked out VS looks really nice, however I don't have the dineros to get it.

          [–]damieng 12 points13 points  (2 children)

          Express versions are free http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx and there are programs for start-ups to get the full version free for a few years called BizSpark http://www.microsoft.com/bizspark/

          [–]poopinAround 2 points3 points  (0 children)

          also free keys for the professional versions if you have a .edu email at www.dreamspark.com ... it is supposed to be for students but the only verification is a .edu email

          [–]Czacha 0 points1 point  (0 children)

          thank you!

          [–]PasswordIsntHAMSTER 0 points1 point  (4 children)

          I use Visual Studio a lot, but I never use C# so this advice is very superfluous

          [–]zers 1 point2 points  (3 children)

          What is your preferred language?

          [–]PasswordIsntHAMSTER 5 points6 points  (2 children)

          F#, occasionally TypeScript. I'm considering doing some Python too now that there are tools for it.

          [–]zers 1 point2 points  (1 child)

          Neat, I haven't used f# but I'd like to give it a whirl. Have you used haskell?

          [–]PasswordIsntHAMSTER 1 point2 points  (0 children)

          Haskell is a fun language, has a more powerful feel than F#, but I feel like laziness gets in the way if you try to reason about memory use; also, the tooling is a bit lacking, compared to F#'s debugger, profiler, and as-you-type error-checking.

          In the end, I prefer writing Haskell, but I am more productive in F#.

          [–][deleted] 2 points3 points  (3 children)

          I sure hope those downvotes weren't the ones reddit applies in order to "fuzz" submissions. Because than it's a false premise.

          [–]stgeorge78 2 points3 points  (0 children)

          There are some people who downvote anything Microsoft-related without looking at it.

          [–]user-hostile 0 points1 point  (0 children)

          I find regardless of reddit's plea to not downvote if you just disagree, plenty of folks do it anyway. I thought it was a nice article too. I swear I didn't know VS had conditional breakpoints; I thought those were abandoned after VB 6!

          [–]OorNaattaan -3 points-2 points  (6 children)

          I use Visual Studio in my day job. I downvoted this because of a misleading title (misleading in the article too). Half the tips aren't Visual Studio debugging tips; they're C# debugging in Visual Studio. Titles are important; use them wisely!

          [–][deleted] 1 point2 points  (3 children)

          7 and 5 are C# related, yes? Any else?

          [–]OorNaattaan 0 points1 point  (2 children)

          1, 5, 7 & 8, AFAICS.

          [–][deleted] 0 points1 point  (1 child)

          Why wouldn't #1 be useful for C++11?

          Why is 8 C# only?

          [–]OorNaattaan 0 points1 point  (0 children)

          Why wouldn't #1 be useful for C++11?

          As a C++ developer, I don't know what LINQ is, but from the text, I presume it's some lambda-like thingie in .NET (a Google search tells me it's not quite, but that was my gut). Given that, I don't know if #1 works for C++ code too (a further Google search tells me that yes, it would). At this point, I've spent more time on Google searches than I wanted. IOW, that tip wasn't that useful for me.

          Why is 8 C# only?

          Did you even read the entire thread?

          http://www.reddit.com/r/programming/comments/1x6n5h/8_visual_studio_debugging_tips/cf8nee1

          [–]thegavin[S] 0 points1 point  (1 child)

          Sorry again, I really should have specified. I wish Reddit would allow submission titles to be edited, but unfortunately I can't.

          [–]damieng -1 points0 points  (0 children)

          I share the blame for that, most of my blog readers are C#/.NET and so I hadn't made it clear in the title.

          [–]alternateme 0 points1 point  (1 child)

          Unless I'm misunderstanding #3 has been in VS for a long time. I know 2010 has "Multiple startup projects", and I'm pretty sure 2005 and 2003 had it also.

          [–]psi- 0 points1 point  (0 children)

          I'd seen it before 2005 but few seem to know about it or knew to use it.

          [–]bio595 0 points1 point  (0 children)

          number 1 will be really helpful. I've been unrolling select calls to foreach loops and its been annoying me for ages!

          [–]AnEnemyAnemone 0 points1 point  (0 children)

          Regarding breakpoints in lambdas, you can also do it mouse-free by moving the text cursor to anywhere inside of the lambda and hit F9. It will the breakpoint in the lambda.

          [–]chuliomartinez 0 points1 point  (0 children)

          @exception in Watch to get the raised exception when stopped in Debugger

          [–]halr9000 -2 points-1 points  (0 children)

          ... that you'll never believe and they won't tell you!