Category: Visual Studio


To do lists are wonderful things. When you have a lot of things to do they help you to organise and prioritise all those things, they give you satisfaction when you cross something off your list, and they give you feedback on how you’re doing and whether you need to put some things on the back burner for a while.

Visual studio offers a built in task list to which you can add user tasks, assign priorities and tick off completed tasks.

User Task List – I wish my real to do list was this empty ūüė¶

There’s more though, the Task List will also scan your code for token comments – which are special comments containing specific strings.


// TODO: Refactor constructors to use constructor chaining

#region Constructors

#endregion

// HACK this works but it is bad practice to make this method public
 public int GetNextAccountNumber()
 {
return m_NextAccountNumber;
 }

By looking at the comments section you can see a TODO comment and a HACK comment that have been picked up automatically by the task list which read the comments in the code. Once the task is done the comment is deleted.

These tasks have been generated by reading comments in the code

You can even specify your own tokens. They say that you shouldn’t worry about making code more efficient until efficiency actually becomes a problem.

Adding a new INEFFICIENT token – it’s low priority though because efficiency isn’t an issue yet

You can add your own tokens by selecting Tools ->Options -> Environment -> Task List. Here I’ve created an INEFFICIENT token, so that if I write code that I know could be more efficient I can give it the INEFFICIENT token, and if I have to look for ways to speed up code later I can look for all the pieces of code that have been given this token.

Now if I ever want to speed up my code I’ll know just where to look!

Advertisements

I had to use a conditional break point recently, and I was very thankful for that feature. I thought it might be worth a quick post to spread the communal joy of conditional break points.

So, I’m assuming with the ideas of a break point. That is, clicking on the bar next to your code and adding a little red dot that will stop your code running (if your in debug mode) and let you take a closer look at what is going on. That’s a really useful feature.

Now suppose that you’re loading a file with 10,000 or so lines, and everything works as you expect it to for 4,000 lines, but then things start going wrong. Here’s an example of what the code might look like:

            string name, addressLine1, addressLine2, starSign;
uint age;

StreamReader reader = new StreamReader(@"D:\Files\files.txt");

while(!reader.EndOfStream)
{
name = reader.ReadLine();
addressLine1 = reader.ReadLine();
addressLine2 = reader.ReadLine();
starSign = reader.ReadLine();
if (!uint.TryParse(reader.ReadLine(), out age))
{
Console.WriteLine("Error parsing age value");
}
}

reader.Close();

It would be really useful if you could stop the code at that point, and take a look at the data when it starts to go wrong, but if you slap a break point inside that while loop it’s going to get hit 4,000 times before you get to where you want to be.
This is a perfect example where you could use a conditional break point. If you right click on the break point you are presented with a menu of options.

Breakpoint right click menu

In this case, you could select Hit Count and the specify when you want the code to break it terms of how many times this breakpoint has been hit.

Stop when this line has been hit 4000 times

Another way of thinking of it would be that you want the break point to remain inactive for the first 3,999 times the code passes it, and then you want the code to stop so you can step through and see what is going on in more detail.
That’s really useful! but what are some of those other options? Might they be just as useful?

Stop if the name read in is Steve!

Well yeah, they are. Condition allows you to type a condition to break on, should it ever happen. In this case the code will break if the name read in is “Steve”.
When Hit allows you to instruct the compiler to do other things when the breakpoint is hit, such as print a message to the Output pane in visual studio – that means no more writing out to the console, assuming that there is a console to write out to!

when hit writer the name and two address lines to visual studio's output window

You can even combine different breakpoints, so for example I could write the addressLine1 value for all the entries with the name “Steve” to visual studios Output pane, it I wanted to:

Output Pane

It’s a very useful feature and if you’re not familiar with it already you should definitely check it out!

 

It’s worth getting to know the little features in your favourite IDE. Here’s a neat trick that might increase your productivity a little.

Suppose I want to write a Hello, world! program.


static void Main(string[] args)

{

Console.Write(" world!");

Console.WriteLine("Hello,");

}

Uh oh, I’ve written a ” world!Hello,” program instead. What a muppet! I need to swap those two lines of code around.

There are a few different ways you could deal with this (albeit trivial) problem using a combination of copying, pasting, cutting and deleting, but did you know that you can copy more than one thing to the clipboard at a time into something called the clipboard ring?

Here’s how it’s done:

Select the ” world!” and Ctrl + X, then select the “Hello,” and Ctrl + X (for speed use the keyboard: cursor keys + Shift)

Press Ctrl + Shift + Ins twice, move the cursor to the Write call at the top and then another Ctrl + Shift + Ins twice and the switch is complete!


static void Main(string[] args)

{

Console.Write("Hello,");

Console.WriteLine(" world!");

}

Okay, so this is a trivial example, but the ability to cycle through stuff on the clipboard could be really useful in the right hands. There used to be functionality to show the contents of the clipboard ring but that appears to have been removed from more recent versions of visual studio. There are third party solutions that replace is, but if anyone knows how to replace the Visual Studio version let me know!