.net

A customized shortcuts scheme for an improved debugging workflow in Visual Studio

Update: I just realized I’d forgotten one of my favorite shortcuts: Alt + N. So what does it do? See the list below! Suffice it to say, after using it I no longer worry about getting lost in tangent code while debugging. 😉

I love a good shortcut. I love it when I can perform a task without removing my hands from their regular location on the keyboard, and without having to look down to find that one special hard-to-reach key. Now as every moderately experienced programmer knows, debugging is often an essential part of the job. In fact it can sometimes take up the clear majority of your time for extended periods. That’s why I’ve always found it so strange that the shortcuts for regular debugging actions such as step over, step into, step out of and so on have such (in my opinion at least) impractical shortcuts.

The following is a scheme of key bindings that I have been using for two or three months now, and which I find natural and easy. The effect of this change has been bigger than I had anticipated: Since I no longer need to pause to look down at the keyboard while stretching for the F11 key for example, my train of thought is slightly less prone to being interrupted. Cutting out these minimal context switches means I can focus better on the code, which means I work faster and spend less mental energy keeping track of where I am, and what is going on — and there’s your improved debugging workflow.

(*) shift + F11 is a common shortcut for “step into”.

Shortcuts

These are the main shortcuts I’ve added. Some are inspired by the use of Vim (J moves the cursor to the next line in Vim – her it steps to the next line of code), while others are based on mnemonics (G for “Go” to either start or continue debugging, S for “Stop“), etc.

Continue reading

“Operation could destabilize the runtime” caused by dynamic invoke.

Dynamically invoking code can sometimes mask errors that actually are pretty simple to fix. If you come across problems like this, it’s probably a good idea to try to reproduce the problem as close to the calling code as possible, in order to see the actual error message, instead of something generic like the title of this post.

Continue reading

Replace a string with newline in emacs

I sometimes find myself wanting to replace certain strings in a text file with a new-line character. Not with “\n” or “\r\n” mind you, but an actual break to make the text more readable in the editor. Take for example a random stack trace from Visual Studio. I sometimes copy these out to another simpler text editor to examine them closer. The problem is that when pasted, they will appear as a single line with “\r\n” instead of actual line breaks:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)\r\n at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)\r\n at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)\r\n at System.Windows.Forms.AxHost.CreateInstance at System.Windows.Forms.AxHost.GetOcxCreate()\r\n at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)\r\n at System.Windows.Forms.AxHost.CreateHandle()\r\n at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n at System.Windows.Forms.AxHost.EndInit()\r\n at MyApplication.frmPrint.InitializeComponent() in (...)

If your text editor breaks lines at the edge of the window, it might look more like this:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& 
clsid, Object punkOuter, Int32 context, Guid& iid)\r\n at 
System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid 
clsid)\r\n at System.Windows.Forms.AxHost.CreateInstanceCore(Guid 
clsid)\r\n atSystem.Windows.Forms.AxHost.CreateInstance at 
System.Windows.Forms.AxHost.GetOcxCreate()\r\n at 
System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)\r\n at 
System.Windows.Forms.AxHost.CreateHandle()\r\n at 
System.Windows.Forms.Control.CreateControl(Boolean 
fIgnoreVisible)\r\n at System.Windows.Forms.Control.CreateControl(Boolean 
fIgnoreVisible)\r\n at System.Windows.Forms.AxHost.EndInit()\r\n at 
MyApplication.frmPrint.InitializeComponent() in (...)

..which may be better, but still not very readable.

Most text-editors have some form of replace-all utility built in, but it’s not always easy to replace “\r\n” with an actual line shift (try it!). In Emacs, there is a nice way to handle this:

Use M-x Replace-String to prompt for the string to replace. Enter “\r\n” in the mini-buffer. Press enter, and it will prompt for the string to replace it with. At this point, use C-q C-j to input a new-line (C-j) as quoted text (C-q), and press enter again.

The result will be a much more readable stack trace:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)
at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)
at System.Windows.Forms.AxHost.CreateInstance at System.Windows.Forms.AxHost.GetOcxCreate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.AxHost.EndInit()
at MyApplication.frmPrint.InitializeComponent() in (...)

Viewing Linqpad scripts in VS2013

I use LINQPad all the time at, both for experimenting with C#, and for building and testing Linq queries before I import them in our projects.

As an example, I recently wrote a few scripts for fetching some data from a database, and reformatting it into JSON data which we will use as dummy-data for testing. This will not be a part of our finished project, but it’s still nice to have the script visible in Visual Studio, even if I only run it through LINQPad .

The following are a couple of tips I’ve found useful when working with .linq-files in or from within Visual Studio.

Continue reading