Visual Studio
Talking about Visual Studio tips and tricks
Quote
Column Guides in Visual Studio
A lot of coding guidelines specify the maximum length for a line of code. For instance in the CLR, Microsoft like to keep lines of code under 110 characters long. Visual Studio has a feature which lets you display a vertical line at the column of your choosing to help visually see when a line is getting too long. This does involve mucking in the registry so the usual disclaimers apply.
To enable this feature, set:
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\Text Editor]
"Guides"="RGB(192,192,192) 110"
The values passed to the RGB function let you specify the color of the line, and the number following tells Visual Studio at what column to display it. The
Snippets and auto-stubs
- Snippets from Intellisense. When you start to type in the editor you’ll get the trusty intellisense drop down. If there is a snippet available then it also be shown, for example as you start to type t..r..y you’ll see the "try" snippet, but how do you invoke it. After much trial and error yuo need to double tab, i.e. with the try snippet highlighted press tab twice in quick succession
- Auto-stubs. When you’re inside a function and you realise you have to going to have write another function and call it, then write the the call to the yet-to-exist function an you’ll see a little block appear under the new function name. Selecting that will automatically create the skeleton code for your new function using the arguments (and datatypes).
- Auto-add "Using". If you add a call to a component you have referenced but don’t specific the full namespace and haven’t yet added it to the "using" statments then a little block will appear under the component. Selecting that will auto-create the correct "using" statement
Navigation shortcuts
Converting VBScript to .net
- Ensure Option Explicit is removed – I don’t need it so I’m dropping it from the VBScript – (?i)\s*Option Explicit
- VBScript functions and Subs – Pesky devils, the problem here is that function need a "As object" value and all routines really should (in my case) have their arguments prefixed with ByRef to be compatible with the VBScript.
Find those routines – (?i)(Function|Sub)\\s*[a-z_][a-z_0-9]*\\s*[(](([\\w\\d]*)|(\\s*,\\s*[\\w\\d]*))*[)]
Remember the name of the routine – (?i)(?<=((Function|Sub)\s*))\w*
Get the arguments – (\((([\w\d]*)|(,\s*[\w\d]*))*)|(,\s*(([\w\d]*)|(,\s*[\w\d]*))*) - So we’ve converted the routine declaration, but VBScript doesn’t (typically) use brackets when calling a routine and VB.NET requires them, so "lucky" we remembered the names of the routines in step 2.
Find any caller to a function that isn’t already using brackets and isn’t simply the assignment of the function result – string.Format(@"(?i)(?<!(Function|Sub)\s*){0}(?!(\s*=)|(\s*\())", routineName) - Adding Namespaces – this one caught me out at first, it was fairly obvious that I needed to import "system" but it took a couple of scratched heads to include "Microsoft.VisualBasic", seems obvious now!
- The next problem was general differences in keywords and types, etc. The most common one was replacing "now()" with "DateTime.Now". This proved an interesting problem since a number of scripts contained code such as "now()-1", so converting that to "DateTime.Now-1" didn’t cut it since .net can’t correctly cast the integer. So (for some reason) I chose to replace those with DateSerial(0,0,x) – (?i)(?<=DateTime\.Now\s*)(-|+)\d"
The other common problem was VBScript variables called "Return", so they needed replacing, again nothing fancy just guessed at a unique name, NB return isn’t a valid exit in VBScript –
replace…(?i)\bReturn\b", "ReturnVarX " - "Set" and "Let" – so Microsoft has finally killed these off, who knew? 😉 – (?i)(?<=\s)set\s*
- All done!
So there you go, as long as you’re not guaranteeing 100% VBScript conversion and, like me, have a finite (if large) number of scripts to convert, you can probably convert them all with the minimum of fuss and a few regular expression.