An interesting thing to note with Visual Studio is that when you select Build, Visual Studio compares the current output of your solution with the source code. If the source code matches that of the code found in the assembly, it will take no action (build not required).
Although this makes sense, Visual Studio doesn't look at other files. Therefore if you alter a resource such as an xml file within your solution, Building it will not incorporate any changes into the output folder. You must specifically choose Rebuild.
Sunday, 2 May 2010
Thursday, 8 April 2010
Creating HTML based CD content with .HTA files
A relatively unheard of file extension (not to be confused with .htaccess), .HTA files are HTML Applications. They allow you to run HTML without all the security features you'll find in your browser, such as pop-up blockers and activex popups.
Although they cannot be used online, they are suited to local content, such as help CDs and application documentation.
You can find more information about HTA files on MSDN: http://msdn.microsoft.com/en-us/library/ms536496(VS.85).aspx
One of the most useful features, apart from their ability to run VBScript and similar, is that you can completely customise the layout of the window, much like a popup window can be stripped of its toolbars. It allows you to create a professional project, that behaves like a software application, without much fuss at all. And if your only content is an iframe, you can host your entire site within the .hta without having to rewrite any of it.
While this technology isn't for everyone, it's a great thing to be aware of, next time you want to include some documentation with your software or even a CD of help content. You could even use it as a menu for your CD, all without writing a scrap of code (HTML excluded, of course).
Although they cannot be used online, they are suited to local content, such as help CDs and application documentation.
You can find more information about HTA files on MSDN: http://msdn.microsoft.com/en-us/library/ms536496(VS.85).aspx
One of the most useful features, apart from their ability to run VBScript and similar, is that you can completely customise the layout of the window, much like a popup window can be stripped of its toolbars. It allows you to create a professional project, that behaves like a software application, without much fuss at all. And if your only content is an iframe, you can host your entire site within the .hta without having to rewrite any of it.
While this technology isn't for everyone, it's a great thing to be aware of, next time you want to include some documentation with your software or even a CD of help content. You could even use it as a menu for your CD, all without writing a scrap of code (HTML excluded, of course).
Wednesday, 3 March 2010
Enhancing the Visual Studio experience with some shortcuts you probably don't know
It's well known to most developers that Visual Studio has a whole plethora of shortcuts available. You can find most of these just by using your search engine.
While there are literally hundreds, and I strongly suggest taking the time to find them, I'll show you the three I use most, and then show you some shortcuts you probably won't know, even if you are a veteran of Visual Studio.
The shortcuts I use most (and probably already know) are:
CTRL + .
If you type something that requires a Using or Import statement, it will get a small red underscore beneath it. You can hover over it with the mouse (which is quite tricky because the cursor is a bar | shape and the underline is a flat _ shape) and view a little menu of suggestions to fix it, for example "using System.Drawing;" which when clicked will fill in the reference for you at the top of the page. It also shows you the shortcut to bring this menu up, which is SHIFT + ALT + F10.
What it neglects to tell you is that CTRL + . works just as well. So while you're typing, you can hit CTRL + . followed by ENTER to add the required 'using' or 'Import' statements automatically without interrupting your typing.
CTRL + K, C
CTRL + K, U
These are key chords, which are shortcuts that require two keypresses. To comment or uncomment some code, highlight it, then press CTRL+K. Keep the CTRL key held, and you will see "(Ctrl+K) was pressed. Waiting for second key of chord..." displayed at the bottom of Visual Studio. Keeping CTRL held down, press either U to Uncomment, or C to Comment your code.
TAB
Tab will tab your code to the left and right, but this also works if you select multiple lines of code to indent (and with SHIFT + TAB, un-indent). Very few developers know this, because we all imagine pressing tab with selected text will overwrite it.
The shortcuts you probably don't know
There are lots of global shortcuts that work in all applications, particularly Microsoft ones such as the Office suite. A lot of them are applicable to Visual Studio as well, but many have been lost over time and are very little-known. They won't be on pages showing you Visual Studio shortcuts because technically they are not VS specific, but they are absolutely applicable and your productivity would increase massively if you take the time to learn them.
The more spectacular ones are at the end.
CTRL + INSERT
SHIFT + INSERT
These are an alternative to CTRL + C and CTRL + V. Anyone that used Microsoft Quick BASIC will know that this was the only way to copy and paste in the IDE, but it's a universal shortcut throughout windows. Now you can copy and paste with the right hand as well as the left, which is massively advantageous as all of the navigation keys you need to whizz through your document are operated by the right hand - the INSERT, HOME, PAGE UP, DELETE, END, PAGE DOWN keys, and the arrow keys.
SHIFT
While simple, the Shift key is most powerful when combined with the other shortcuts below, so I have listed it first. Shift is used for selecting an area of text, most commonly by pressing SHIFT + Arrow keys. It's important to note that it can be used with combinations of other keys. Pressing SHIFT + END and SHIFT + HOME is incredibly useful yet so often overlooked.
Another trick is when you have selected a large block of text with the mouse and released the mouse button, you can press and hold SHIFT and continue to manipulate the selection. This is very useful if you select a large block of text with the mouse, then decide you want to deselect a few characters from the end, or if the selection includes a carriage return.
CTRL + HOME
CTRL + END
These shortcuts position the cursor at the start or end of a document. Remember, you can use them with SHIFT key.
CTRL + PAGE UP
CTRL + PAGE DOWN
These shortcuts move the cursor to the top or bottom of the window, without scrolling your document up or down. They work with SHIFT.
CTRL + UP
CTRL + DOWN
These shortcuts are specific to Visual Studio, but allow you to scroll up and down the page without the cursor moving.
CTRL + LEFT
CTRL + RIGHT
These are probably the most underused and yet most powerful shortcuts. They move the cursor one word to the right or left. It took me a few days of forcing myself to use these shortcuts until they became second nature, and the payoff is considerable. Combined with SHIFT, you can now manipulate code considerably faster than holding an arrow key and watching the cursor crawl along.
CTRL + BACKSPACE
CTRL + DEL
This works in some editors and Visual Studio, and works as above but deletes one word before or after the cursor.
CTRL + -
This is a Visual Studio shortcut to navigate back, and it has a corresponding button in the toolbar by default. The chances are, using all of these shortcuts at high speed, at some point or another you will accidentally end up in the wrong place, usually by pressing CTRL+HOME or CTRL+END. Simple press CTRL + - and the cursor will magically jump back to where it was last, so you can continue.
It's important to remember there are SHIFT and CTRL keys on both sides of the keyboard. This makes it considerably easier to press the shortcuts, especially when doing something tricky such as CTRL + SHIFT + Right Arrow to select the words one by one immediately to the right of the cursor. You can press Left CTRL, Right SHIFT, and the arrow key without any kind of unnatural movement of the hands.
Try these shortcuts and stick with them for a few days, and you'll be zipping around Visual Studio faster than ever before!
While there are literally hundreds, and I strongly suggest taking the time to find them, I'll show you the three I use most, and then show you some shortcuts you probably won't know, even if you are a veteran of Visual Studio.
The shortcuts I use most (and probably already know) are:
CTRL + .
If you type something that requires a Using or Import statement, it will get a small red underscore beneath it. You can hover over it with the mouse (which is quite tricky because the cursor is a bar | shape and the underline is a flat _ shape) and view a little menu of suggestions to fix it, for example "using System.Drawing;" which when clicked will fill in the reference for you at the top of the page. It also shows you the shortcut to bring this menu up, which is SHIFT + ALT + F10.
What it neglects to tell you is that CTRL + . works just as well. So while you're typing, you can hit CTRL + . followed by ENTER to add the required 'using' or 'Import' statements automatically without interrupting your typing.
CTRL + K, C
CTRL + K, U
These are key chords, which are shortcuts that require two keypresses. To comment or uncomment some code, highlight it, then press CTRL+K. Keep the CTRL key held, and you will see "(Ctrl+K) was pressed. Waiting for second key of chord..." displayed at the bottom of Visual Studio. Keeping CTRL held down, press either U to Uncomment, or C to Comment your code.
TAB
Tab will tab your code to the left and right, but this also works if you select multiple lines of code to indent (and with SHIFT + TAB, un-indent). Very few developers know this, because we all imagine pressing tab with selected text will overwrite it.
The shortcuts you probably don't know
There are lots of global shortcuts that work in all applications, particularly Microsoft ones such as the Office suite. A lot of them are applicable to Visual Studio as well, but many have been lost over time and are very little-known. They won't be on pages showing you Visual Studio shortcuts because technically they are not VS specific, but they are absolutely applicable and your productivity would increase massively if you take the time to learn them.
The more spectacular ones are at the end.
CTRL + INSERT
SHIFT + INSERT
These are an alternative to CTRL + C and CTRL + V. Anyone that used Microsoft Quick BASIC will know that this was the only way to copy and paste in the IDE, but it's a universal shortcut throughout windows. Now you can copy and paste with the right hand as well as the left, which is massively advantageous as all of the navigation keys you need to whizz through your document are operated by the right hand - the INSERT, HOME, PAGE UP, DELETE, END, PAGE DOWN keys, and the arrow keys.
SHIFT
While simple, the Shift key is most powerful when combined with the other shortcuts below, so I have listed it first. Shift is used for selecting an area of text, most commonly by pressing SHIFT + Arrow keys. It's important to note that it can be used with combinations of other keys. Pressing SHIFT + END and SHIFT + HOME is incredibly useful yet so often overlooked.
Another trick is when you have selected a large block of text with the mouse and released the mouse button, you can press and hold SHIFT and continue to manipulate the selection. This is very useful if you select a large block of text with the mouse, then decide you want to deselect a few characters from the end, or if the selection includes a carriage return.
CTRL + HOME
CTRL + END
These shortcuts position the cursor at the start or end of a document. Remember, you can use them with SHIFT key.
CTRL + PAGE UP
CTRL + PAGE DOWN
These shortcuts move the cursor to the top or bottom of the window, without scrolling your document up or down. They work with SHIFT.
CTRL + UP
CTRL + DOWN
These shortcuts are specific to Visual Studio, but allow you to scroll up and down the page without the cursor moving.
CTRL + LEFT
CTRL + RIGHT
These are probably the most underused and yet most powerful shortcuts. They move the cursor one word to the right or left. It took me a few days of forcing myself to use these shortcuts until they became second nature, and the payoff is considerable. Combined with SHIFT, you can now manipulate code considerably faster than holding an arrow key and watching the cursor crawl along.
CTRL + BACKSPACE
CTRL + DEL
This works in some editors and Visual Studio, and works as above but deletes one word before or after the cursor.
CTRL + -
This is a Visual Studio shortcut to navigate back, and it has a corresponding button in the toolbar by default. The chances are, using all of these shortcuts at high speed, at some point or another you will accidentally end up in the wrong place, usually by pressing CTRL+HOME or CTRL+END. Simple press CTRL + - and the cursor will magically jump back to where it was last, so you can continue.
It's important to remember there are SHIFT and CTRL keys on both sides of the keyboard. This makes it considerably easier to press the shortcuts, especially when doing something tricky such as CTRL + SHIFT + Right Arrow to select the words one by one immediately to the right of the cursor. You can press Left CTRL, Right SHIFT, and the arrow key without any kind of unnatural movement of the hands.
Try these shortcuts and stick with them for a few days, and you'll be zipping around Visual Studio faster than ever before!
Tuesday, 2 March 2010
C# to Visual Basic Conversion - The agent process was stopped error
As a C# developer, I steer clear of Visual Basic and merrily skip past the code samples in MSDN tagged [Visual Basic], searching for something my brain can understand. I'm sure there are a lot of developers like myself out there who do the same. We learned C#, or C++, or Java (or even PHP or Javascript) - all of which are syntactically very similar - and this caused us to choose Visual C# over Visual Basic when adopting the new .NET technology.
C# is a relatively new language. And it's entirely possible that in the past, you used to dabble in BASIC. I first programmed in Amstrad BASIC on my trusty CPC464, AMOS on the Amiga, QBasic on the PC, then Visual basic 3, 5, 6... and then C#.
In BASIC, a popular way of combining a string would be to use the + operator.
For example:
I used this technique with QBASIC, Visual Basic 3-6, and never had any issues. It also works in C#
It even works in Visual Basic
This code will not give you a compile error. It will run and work as expected. However it is not the recommended method of doing it. This is because it sometimes doesn't work.
Looks good. In C#, this syntax would definitely work. However if you run this in Visual Basic, you will get a runtime error.
Conversion from string "Your age:" to type 'Double' is not valid.
It seems to handle + as an arithmetic operator. Why is this useful? I have no idea. It's something to note though, that Visual Basic treats + as a way to combine numbers, unless you are combining two strings.
Incidentally,
will run fine and give you 51. I just can't imagine a situation where you'd ever do this.
What really bugs me is the consistency. If + converts things to Double, why does combining two strings "Hello" and "World" work?
All I can say is be careful, especially when converting code. If you do what I do and copy and paste C# code into your VB solution then fix errors, this problem won't flag up until runtime, when it may or may not work.
The correct way to handle this problem in Visual Basic is to use the & operator instead. Simple as that.
The result is
Your age: 26
2526
Now, onto the reason why this caused me so much grief...
I was writing some unit tests for a piece of code, and that code involved lambda expressions. This is a pretty new thing, especially as far as Visual Basic is concerned (it's generally one step behind C# for new features).
When you're testing, generally your tests either pass or fail. However, just occasionally you may get the dreaded error:
The agent process was stopped
Your test will mysteriously break with this error, and no further information. In my case, I discovered this was to do with a BackgroundWorker that was hooked into a lambda expression. After running it asynchronously, and doing an e.WaitOne to wait for it to return, it didn't. This caused the test to stop with this error. There was no futher information.
Trying to run the test in debug mode didn't do anything at all; Visual Studio did nothing. Manually bringing up the test window showed the test had been aborted.
Taking a step back:
BackgroundWorker allows you to run some code asynchronously (in the background) so you can get on with other things. Useful for if something takes a while to do.
More information about BackgroundWorker on MSDN
Lambda expressions are a vast topic, but in this case I can create an event handler, and instead of pointing it at a method (such as button_click), I can define the code myself, which is faster and neater especially if its just one or two lines.
More information about Lambda expressions on MSDN
For example:
So it turns out, that if your lambda expression generates a runtime error, you don't get any feedback on it. You don't get the usual exception dialogue appearing and showing you what went wrong. Instead, your test produces "The agent process was stopped" and leaves you wondering why.
Solution
These two topics tie together because my lambda expression contained this code:
As soon as I changed the + into a &, the test error went away.
So if you're reading this because you're getting "The agent process was stopped", it's quite possibly a threading issue. In my case, the lambda expression generated a runtime error which caused it to never call the OnDataModificationCompleted method and let the BackgroundWorker know that the process had finished executing. The other part of my code sat waiting indefinitely, and made the test method generate that error (rather than passing or failing).
C# is a relatively new language. And it's entirely possible that in the past, you used to dabble in BASIC. I first programmed in Amstrad BASIC on my trusty CPC464, AMOS on the Amiga, QBasic on the PC, then Visual basic 3, 5, 6... and then C#.
In BASIC, a popular way of combining a string would be to use the + operator.
For example:
' BASIC
PRINT "HELLO " + "WORLD"
I used this technique with QBASIC, Visual Basic 3-6, and never had any issues. It also works in C#
// C#
Console.WriteLine("Hello " + "World");
It even works in Visual Basic
' Visual Basic
Console.WriteLine("Hello " + "World")
This code will not give you a compile error. It will run and work as expected. However it is not the recommended method of doing it. This is because it sometimes doesn't work.
' Visual Basic
Dim age As Integer = 26
Console.WriteLine("Your age: " + age)
Looks good. In C#, this syntax would definitely work. However if you run this in Visual Basic, you will get a runtime error.
Conversion from string "Your age:" to type 'Double' is not valid.
It seems to handle + as an arithmetic operator. Why is this useful? I have no idea. It's something to note though, that Visual Basic treats + as a way to combine numbers, unless you are combining two strings.
Incidentally,
' Visual Basic
Dim age As Integer = 26
Console.WriteLine("25" + age)
will run fine and give you 51. I just can't imagine a situation where you'd ever do this.
What really bugs me is the consistency. If + converts things to Double, why does combining two strings "Hello" and "World" work?
All I can say is be careful, especially when converting code. If you do what I do and copy and paste C# code into your VB solution then fix errors, this problem won't flag up until runtime, when it may or may not work.
The correct way to handle this problem in Visual Basic is to use the & operator instead. Simple as that.
' Visual Basic
Dim age As Integer = 26
Console.WriteLine("Your age: " & age)
Console.WriteLine("25" & age)
The result is
Your age: 26
2526
Now, onto the reason why this caused me so much grief...
I was writing some unit tests for a piece of code, and that code involved lambda expressions. This is a pretty new thing, especially as far as Visual Basic is concerned (it's generally one step behind C# for new features).
When you're testing, generally your tests either pass or fail. However, just occasionally you may get the dreaded error:
The agent process was stopped
Your test will mysteriously break with this error, and no further information. In my case, I discovered this was to do with a BackgroundWorker that was hooked into a lambda expression. After running it asynchronously, and doing an e.WaitOne to wait for it to return, it didn't. This caused the test to stop with this error. There was no futher information.
Trying to run the test in debug mode didn't do anything at all; Visual Studio did nothing. Manually bringing up the test window showed the test had been aborted.
Taking a step back:
BackgroundWorker allows you to run some code asynchronously (in the background) so you can get on with other things. Useful for if something takes a while to do.
More information about BackgroundWorker on MSDN
Lambda expressions are a vast topic, but in this case I can create an event handler, and instead of pointing it at a method (such as button_click), I can define the code myself, which is faster and neater especially if its just one or two lines.
More information about Lambda expressions on MSDN
For example:
// C#
bw.DoWork += (o, args) =>
{
// Code here
}
' Visual Basic
AddHandler bw.DoWork,
Sub(o, args)
' Code here
End Sub
So it turns out, that if your lambda expression generates a runtime error, you don't get any feedback on it. You don't get the usual exception dialogue appearing and showing you what went wrong. Instead, your test produces "The agent process was stopped" and leaves you wondering why.
Solution
These two topics tie together because my lambda expression contained this code:
' Visual Basic
OnDataModificationCompleted(True,
"Create new employee with ID: " + DirectCast(args.Result, Employee).ID,
DirectCast(args.Result, Employee).ID)
As soon as I changed the + into a &, the test error went away.
So if you're reading this because you're getting "The agent process was stopped", it's quite possibly a threading issue. In my case, the lambda expression generated a runtime error which caused it to never call the OnDataModificationCompleted method and let the BackgroundWorker know that the process had finished executing. The other part of my code sat waiting indefinitely, and made the test method generate that error (rather than passing or failing).
Saturday, 20 February 2010
Every saga has a beginning...
This blog will host little gems of knowledge that I come across in my day-to-day life. With any luck they will be indexed on popular search engines and help other programmers who are struggling with some of the issues I have faced off against.
Subscribe to:
Posts (Atom)