Looking to be a little more productive in Visual Studio!? Take a gander at these great tips I’ve compiled over my years of working in Visual Studio. I might have some more specialized tips coming up, but these ones should apply to anyone using Visual Studio and I’m guessing more than a few people don’t know these.
Auto formatting code
Hate how when you copy/paste code the indentation is all wrong? Or how about when you hand write some code or markup, but little things are out of sync, even with intellisense? Like the spacing between statements, the position of curly braces, etc. Just use these shortcuts! They automagically reformat code and markup to be consistent. It even works on markup pages. The only times it doesn’t work is if there are some errors in the code/markup. You can also access these shotrcuts directly from the Edit menu.
Format Document: ctrl + k, ctrl + d
Format Selection: ctrl + k , ctrl + f
Context menu
Sometimes you know the name of a class, but don’t have the appropriate using statement added to the page. Take StringBuilder for example. Just type the class name, correctly cases, and then use this shortcut! It will bring up the context menu that is usually hard to see (it’s a thin red outline until you mouse over it). Then you can select the using statement you want (usually the first) and hit enter! That’s it! No more scrolling to the top of the page to add using statements! This also works in other situations where the context menu would normally be used, like when you rename a variable.
Bring up the context menu: ctrl + .
Quick keyboard commenting
Hate adding comments to each line individually, or moving your cursor around to add a block comment? Just use these shortcuts! The first one comments out any lines selected. You don’t even have to select the whole line! Just select a portion and the comment will be added to the beginning of the line! Alternatively, use the second shortcut to remove any comments.
ctrl + k, ctrl + c
ctrl + k, ctrl + u
Cycle through your past clipboard items
Ever cut/copy something, only to accidentally cut/copy over the clipboard? Say no more! Just hold down shift while using the standard paste shortcut to cycle through all of the items that you have cut or copied in Visual Studio. Only works inside Visual Studio though.
Cycle through the clipboard buffer: ctrl + shift + v
Collapse your code to get a birds eye
Ever need to quickly take stock of how your code is layed out? Just use the first shortcut, to collapse all the code into method stubs and code regions! Then you can easily reorganize your methods and properties. When you’re done, feel free to use the second shortcut to expand everything again.
Collapse all code regions: ctrl + m, ctrl + o
Expand all code regions: ctrl + m, ctrl + p
Everyone knows that you can’t create an extension method that uses the same name and parameter list as an existing method of the type your extending. So for example, this method would be perfectly valid:
public static string IsNullOrEmpty(this string text)
{
return string.IsNullOrEmpty(text);
}
Why? Well, for one, the method parameters are different. The existing IsNullOrEmpty method takes a string as an argument, and my extension method does not. Also, the existing IsNullOrEmpty method is static…
WAIT! That doesn’t matter! Check this out:
public static string Format(this string me, params object[] args)
{
return string.Format(me, args);
}
This doesn’t work! That’s right! If there is an existing static method with the same name and parameter list, you will get a compile time error! When you attempt to use the extension method like so:
"My name is {0}".Format(person.Name);
This is the error that ensues:
Member ’string.Format(string, object)’ cannot be accessed with an instance reference; qualify it with a type name instead
Yep. This may seem obvious to some, but not me! Why would it matter that there is a static version of the method, when clearly we’re not using it! I’m guessing it has something to do with the way the compiler looks up methods.
So how do you get around this? Rename your extension method.
One thing I’m also not sure about now, is whether or not you can have 2 methods with the same signature, but have one be static and another be instance-level. Anyone know the answer to that one?
**UPDATE**
Jared, my coworker pointed out something. It looks like having an extension method with the same parameter list is valid. However, for some reason, you can’t use the extension method under certain situations? For example, when creating a String Format() extension method, you can’t actually pass in a string a parameter.
int aNumber = 0;
string aString = string.Empty;
var s1 = "{0}".F(aNumber); // Good, different method name
var s2 = "{0}".F(aString); // Good, different method name
var s3 = "{0}".Format(aNumber); // Good, passing a number as the parameter
var s4 = "{0}".Format(aNumber.ToString()); // compile time error
var s5 = "{0}".Format(aString); // compile time error
Does anyone know why this matters? The method signature for string.Format() is this:
public static string Format(this string me, params object[] args)
So it shouldn’t really matter what you pass in as “args”. Each argument should be boxed as an object, right?
We needed to truncate a summary down to n number of characters to provide a uniform look and feel. So I whipped up this extension method to take the current string and truncate to n characters, attach an ellipsis to the end, and optionally keep the last word of the string preserved. In other words, don’t chop the last word in half.
However, this is a very simple implementation. There are a lot of things that we don’t consider here. For example, we don’t consider the type of text. If the text is HTML, we may leave it in a non-compliant state. Or, if the initial truncated text ends in a period, we don’t leave it as is. So there is a lot of room for improvement here.
Lastly, one thing I’m not doing is highlighting my code… the below sample code probably looks like crap inside this page. I do have a syntax highlighter installed, but I don’t remember how to use it right now. So I’ll fix that later if I remember.
Here is the code:
public static string Truncate(this string text, int length, string ellipsis, bool keepFullWordAtEnd)
{
if (text.IsNullOrEmpty()) return string.Empty;
if (text.Length < length) return text;
text = text.Substring(0, length);
if (keepFullWordAtEnd)
{
text = text.Substring(0, text.LastIndexOf(' '));
}
return text + ellipsis;
}
Microsoft’s Marketing is a bit strange at times, as seen in this video:
But overall, I think they usually have the right message, however obscured it may be.
Personally, I love their slogan for Visual Studio:
Defy All Challenges
And really, that is what being a programmer is all about! We face challenges of all magnitudes every single day. If you didn’t like challenges, you probably wouldn’t have chosen your current profession, or at least you wouldn’t like it!
As much as we may wish for easy days, challenges are what make our jobs interesting.
Back in May I installed WordPress on my shared PHP hosting. I remember it being a fairly simple setup, and I configured it and started posting without issues. Eventually, a notice started popping up that I needed to upgrade. I assumed it meant I needed to:
- Download the package
- Unzip the package
- Upload/replace files
- Run a script
What the upgrade process really entailed was to:
- Click a button!
Yes really, it was that easy! I’m guessing that some people might run into issues if their hosting permissions aren’t setup correctly, but my experience was perfect! Never in a million years did I think using a web-based app would be so easy, at least from the admin side. I’ve dealt with many a web application, and frankly, have yet to see a good upgrade process until now. It didn’t matter if a product was hosted or not, big or small, people don’t always build their applications with upgrades in mind. Take Adobe Acrobat, now known as Adobe Reader, which isn’t even a web app. I’m guessing they changed the name because people hated Acrobat so much! I for one hated Acrobat because anytime I tried to install their “automatic” updates, it failed. Maybe I’m just a skeptical programmer, but I was thinking that upgrading WordPress would be an arduous process. Boy was I wrong!
So next, I started thinking if the upgrade was that easy, what about the plugins? One of my coworkers who also uses WordPress mentioned that the plugin system was good, and that there were some very useful plugins. So I went to the plugins tab and was able to install a few plugins without issue. Here is what I’m currently using (in no particular order):
- Google XML Sitemaps
- Ultimate Google Analytics
- GZIP Output
- Blog in Blog
- SI Captcha Anti-Spam
- WP CSS
I think they are all pretty self-explanatory, with the exception of the Blog In Blog. That plugin allows me to designate a content page as a mini-blog for posts from a specific category! I was a little disappointed that WordPress didn’t have something like that built-in, but this plugin seems to do the trick! Also, the category that is displayed on the content page is removed the homepage, which is perfect for me! Just in case you want to know, I’m setting up a mini-blog on said content page that will be dedicated to our baby! There probably isn’t anything there if you’re reading this, but feel free to make a mental note and check back later. The link for it is on the top of the sidebar
Now back to the plugins. Again, their purpose is self-explanatory, but I really want to reiterate their simplicity and usefulness. WordPress by itself is a decent blog engine, but with all of those plugins it’s practically a CMS. I’m sure there are some more robust plugins out there, I just haven’t had the time to look! But using only the plugins I mentioned above, my site:
- Is ultra-fast (check FireBug, Y-Slow or Chrome if you don’t believe me)
- Is indexed by all the major search engines
- Supports “multiple” blogs
- Is tracked by a top-notch analytics package.
- Is protected by a captcha.
That last point is an especially important one for me. I get emails everyday about people posting comments or registering for my blog. Every time I see those emails, I get excited that somewhere in the world, someone is trying to communicate with me about one of my personal passions, programming. Unfortunately many of these emails are from blog spam, and like 99.99% of the people on Earth, I hate Spam. So hopefully starting today I will no longer get my hopes up when I see an email from my Blog
So that is all for now! Hopefully this inspired someone to go and try out WordPress, especially if they already have some PHP hosting. If you want to know more about the blog platform, visit the WordPress site, do some googling, or post a comment here! If I end up finding some time to checkout more plugins or customize WordPress more, I’ll let you know!
It was great! This was my first code camp, and I LOVED IT! It was at the Microsoft office in Waltham, MA, just outside the Boston area.
A big thanks to all who presented, and to those who organized!
Here are the sessions I attended:
- John Bowen: Visual Studio 2010 features
- Joseph Hill: Mono and .NET developers
- Phil Denoncourt: Localization and Globalization
- Julia Lerman: Entity Framework POCO
- Michael Cummings: ASP.NET MVC Better Practices
- Milan Negovan: The Microsoft AJAX JavaScript library
- Joseph Anderson: E-Commerce with .NET
All the presentations were great, but I bolded the two speakers whose presentations were particularly enlightening for me.
Julie’s presentation was great because I personally and professionally use the Entity Framework, and find it to be a good fit for most of my needs. I usually use it for rapid application development and I find it useful because it’s free, it’s integrated into Visual Studio and it’s fairly simple.
People say the current version of the Entity Framework does have shortcomings, but I personally haven’t run into any of the problem scenarios. However, the new version, Entity Framework 4, is going to be released with .NET 4 and is supposed to add a lot of features that people have been asking for! One of those features is the ability to use POCO’s with the Entity Framework.
Currently, the classes that are generated by the Entity Framework’s designer are coupled with the API. This has a number of implications, one of which is limited testability. This is where POCO comes in. POCO stands for Plain Old CLR Object. Basically, a POCO is an object that does not have external dependencies. So when you are able to use POCO’s with the Entity Framework, you will be decoupling your classes. There are also some other great features in Entity Framework 4, so go check it out!
Phil’s presentation on Localization was great because I am about to completely localize a web application for a friend. To be honest, I didn’t know a whole lot about how localization works in .NET. As I’ve worked with many CMS’ before, I vaguely knew about resource files and about the Thread.CurrentThread.CurrentUICulture property. However, I didn’t know about all the built-in localization features that the .NET framework has. Think about it! With localization in .NET you have to worry about localized content in Master pages, pages, user controls, databases, JavaScript, and more! There are also some gotchas that are good to know before even starting the localization process. Maybe I will talk to Phil and see if I can re-post or re-link the slides he used for his presentation!
I’ll try update this post later with more detail about the sessions if I have more time. Maybe I can link some of the content too.
I can’t take it anymore!
Why do people post bad profile pictures for their blogs and articles!!! I know a person’s appearance have nothing to do with their abilities, but come on! They’re not even trying!
Why post some random picture of yourself looking angry, or in the middle of doing something, or with your face 2 inches from your web cam. Do programmers ever smile!?
So actually, it does make me think less of them because they’re not smart enough to take a half-decent picture!
Here are some examples:
Kirk Knoernschild has an interesting article on his blog about not having a set target date.
Wouldn’t that be grand? Screw the requirements! Screw the time line! Just get to work and start coding. It’s definitely a thought provoking piece, as that’s why I’m writing about it right now!
It has some great ideas. The customer gets what they want, when they want it (more or less). Also, the project team isn’t bothered with time lines and deadlines so most of the pressure is off. Sounds great!
But then reality kicks in. Let’s do a little QA:
Why do we need requirements?
To put it plainly, requirements are what is required of the system. The features, as well as the limitations. As soon as you remove the requirements, its like trying to hold a bottle of shampoo in your hand… without the bottle. The Developers would lose site of their goal, and the customer would keep trying to move the goal!
A better approach than “screw the requirements” would be to constantly get feedback from the customer, and consider what they want and what they need. In a perfect world, a customer knows what he wants, and the developer knows exactly how to implement it. Unfortunately that isn’t always the case!
More on this later if I have time!
So I decided to try and use this as a storage mechanism for my snippets of code. Here is the first one! How to uninstall and reinstall ASP.NET! You might need to do this if you start having issues with ASP.NET.
C:\>cd windows\Microsoft.NET\Framework\v2.0.50727 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -u Start uninstalling ASP.NET (2.0.50727). .................................. Finished uninstalling ASP.NET (2.0.50727). C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -i Start installing ASP.NET (2.0.50727). ....... Finished installing ASP.NET (2.0.50727). C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>
So I was creating a class to do handle the CRUD operations for a type when I was trying to figure out how to return the results. I could…
- Return a bool, and just assume that the problem was with the database. But then I don’t have the actual cause, and any detailed information.
- Return a bool, and use out parameters to store the error. Then check the out parameters in the case that the return value is false. But that requires me to create a variable to pass in as the out parameter.
- Let the exceptions be thrown, and use a try catch in the calling code.
- Do something completely different! How about create a new generic type to use as the return value? All I need is a bool for the outcome of the operation, a string for error codes, and whatever else we want!
I like option 4
While this doesn’t seem like a big deal for my code, since I know what my other code is doing, I feel like this has potential. What about using this code in an actual API fashion? That way, the user of the API doesn’t need to know what type of exceptions are going to be thrown. The only direct advantage I see is that the exceptions are defined types. But I really haven’t thought about it that much…
I will try and post some code if I ever getting around to actually implementing this.
