Use PreviousPageType to Simplify Page Data Access

(Editor's Note: This article has been updated to correct errors in the code.)

In an earlier post on cross-page posting, a reader pointed out that I hadn't discussed the PreviousPageType directive, which can eliminate the need for using the awkward FindControl method (for more on the difficulties of using FindControl -- especially with pages using a Master Page -- see one of the first Practical ASP.NET columns I wrote). Here's how to use PreviousPageType.

More

Posted by Peter Vogel on 03/12/20120 comments


Free Tool: A Better Visual Studio Command Prompt with Console 2.0

Console 2.0 was created by Marko Bozikovic. OK, it isn't a .NET Framework development tool, but I love it. Console is everything that the Windows or Visual Studio Command Prompt should be. Thanks to growing up in MS-DOS I know all the tricks in the console window (how to paste text from other windows, how to scroll up and edit previous commands), but Console is just so much better than knowing those tricks that I happily abandoned them. Console 2.0 provides multiple tabbed windows (so you can work on more than one problem at once), a much easier copy-and-paste process, and the ability to resize it horizontally to reduce line wrap.

More

Posted by Peter Vogel on 02/28/20122 comments


Organize Big Solutions with Solution Folders

If you have a solution that contains multiple projects (I had one solution with 14 projects), you should probably be using Solution Folders. Solution Folders recognize that the projects in a solution often belong in groups: keeping the Web projects separate from the middle-tier business projects, or keeping a class library close to its test project. Solution Folders let you make those groups explicit in Solution Explorer.

To add a Solution Folder to Solution Explorer, right-click on the Solution line at the top and select Add | New Solution Folder. This will add the folder and let you name it. After you've added your Solution Folder, you can drag projects already in Solution Explorer into the folder. You can then expand or collapse each Solution Folder, or even unload a folder if you don't need that part of solution right now and don't want to waste time rebuilding those projects. You can also right-click on a Solution folder and pick Build to compile just the projects in that folder.

This isn't a feature that you need if you keep the number of projects in a solution small; but once you pass some threshold number of projects (and that number will vary from one developer to another), you'll be glad it's there.

Posted by Peter Vogel on 02/24/20121 comments


Guru Tips: Making ToString Useful, Plus Not Getting Fooled by Closure

Julian Bucknall is the Chief Technology Officer at DevExpress. I asked him if he had a tip for .NET developers. Julian said he had two (and that doesn't count the bonus he threw in).

First Tip: ToString() is your friend. When the CLR team wrote System.Object, they included the very minimum of methods needed for all objects. All of them are 100 percent vital, no fat here. One of them is ToString(), the method that provides a human-readable string of the important properties in your class -- especially important for debugging. The default is boring and uninformative: you should work on creating your own overrides for your own classes. Your fellow developers will thank you, since they won't have to go spelunking through your class' properties when debugging -- they can just call your ToString method.

More

Posted by Peter Vogel on 02/16/20121 comments


Using Dynamic Classes to Pass Data

In an earlier tip on using tuples to return multiple values from a method, several readers noted that the tuple's property names (Item1, Item2) left a lot to be desired -- you couldn't really call those names "self-documenting". As a fan of Really Obvious Code (ROC Rocks!), I should point out that there's a solution: using a Dynamic class. As with tuples, the goal here is to create a way to return multiple values from a method without defining a whole new class with its attendant properties and then using that class in exactly one place (returning values from a method) for just a few milliseconds.

More

Posted by Peter Vogel on 02/13/20126 comments


Free Tool: Beyond Testing with Browsers

In a previous free tool column , I recommended WoVS Default Browser Switcher, which provides an easy way to change your default browser when testing ASP.NET pages. However, browsers are only one kind of user agent that can access your page (I do some search engine optimization work -- primarily for professional companies like law firms -- so working with search agents is important to me). More

Posted by Peter Vogel on 01/30/20120 comments


Creating a Useful Task List in Visual Studio

What developers like to do best is to write code (and design the applications that will let them right code). Administrative overhead just gets in the way of writing code—even something as simple as a To Do list. Yet developers are constantly getting part way through a problem and having to go and work on something else, or to address another part of the application before returning to finish what they were working on.

The Visual Studio Task List actually helps with this. Just type a comment that begins with ToDo (e.g. 'ToDo or //ToDo) and is followed by some text, and a line with your text appears in the Task List. To see the Task List, from the View menu, and select Task List. When the Task List window displays, switch the drop down list at its top to Comments. Once you see the list of notes that you (or someone else) has left on the Task List, double-clicking on an item in the list takes you to that section of your application.

In addition to ToDo, there are two other tokens: HACK (for…) and UNDONE (for…). However, if you (or your team) wants to mark additional types of material, you can create your own custom tokens. Select Options From the Tools menu, and in the Options dialog, expand the Environment node to find the Task List entry. Enter a name for your token in the Name text box and click the Add button to define your token (if your token will be used in a case-sensitive language, make life easy for the token's users by making the name either all uppercase or all lowercase).

Unfortunately, there's no easy way to share custom tokens among a team. You can export your settings (which include custom tokens) into a file so that other developers can import your custom tokens…but it's probably just as easy for each developer to set up any new tokens you invent in their copy of Visual Studio (besides, then the developer will know what tokens are available).

Visual Studio 2010 displays all your tokens when you open a project -- earlier versions wouldn't populate the Task List until you opened the file containing the token. However, in earlier versions of Visual Studio, if you do a Find and Replace replacing "ToDo" with "ToDo" and click the Replace All button, Visual Studio would find all of the files containing a ToDo token and open them for you, updating the Task List as it goes.

Posted by Peter Vogel on 01/30/20121 comments


ASP.NET: Integrating Search and Help with Cross-Page Posting

By default, ASP.NET pages post their data back to themselves. However, there are cases that, when the user clicks on a button, you want the data in the browser sent to some other page. For instance, if you have an option that lets users search the site, you've probably implemented this with a Search button with a text box on every page on your site. When the user enters some text and clicks on the button, you want to send the user to your site's Search page, not back to the current page—and you also want to send to that page whatever search term the user entered in the text box beside the button. The same is true of a Help button with a text box: When the user clicks the button, you want to send the user to a page that displays help about the topic the user entered into the text box. More

Posted by Peter Vogel on 01/13/20123 comments


Free Tool: WoVS Quick Add Reference

When I tell people what I like about productivity tools like ReSharper and CodeRush (among others), I always mention their support for automatically adding references and adding imports for namespaces when I type in a class name. The Clarius team, through World of Visual Studio (WoVS), has a free add-in that does that also. But sadly, I was only able to get it to work for C#.

Here's what happens: You type in a class name that Visual Studio doesn't recognize because you don't have the right imports/using statement at the top of your file; or worse yet, you haven't added a reference to the library to your project. You get the red wavy line and the error tooltip says that the type or namespace can't be found.

Here's what you do: Click at the front of the class name to get the error correction dropdown list. Provided you've spelled and capitalized the class name correctly, you'll have a new choice at the bottom of the dropdown list. That new choice automatically adds a reference to the library you need (there may be multiple entries if the class name appears in several libraries) and then goes on to insert the necessary using statement at the top of your file. To put it another way: One click and your code will compile.

The tool is even easy to install -- you can get it from Visual Studio's Extension Manager. The only downsides? As I said, it only works for C# and, I'm told by some international friends, only if Visual Studio is running in English. Plus, of course, I'll no longer know the namespace or library for any class that I'm using. It turns out that I'm OK with that last one.

Posted by Peter Vogel on 12/14/20111 comments


Return Multiple Values from Methods with Tuples

You have a method that returns multiple values. So you have to define a class, instantiate it, set the properties to the values, and return the resulting object, right? Well, no you don't -- at least in .NET Framework 4, which gives you a simpler way: Tuples.

A Tuple can hold up to eight values (including classes) and can be created on the fly. To declare and instantiate a Tuple that could hold two values (one string and one integer), you'd use code like this:

  Dim tup As New Tuple(Of Integer, String)

When you instantiate a new tuple, you also pass the values that will be put in the Tuple. So instantiating the Tuple and putting the number 2 and the string "fred" in it would require this code:

  Dim tup As New Tuple(Of Integer, String)
              (2, "fred")

There's no need to define a special purpose class to return some arbitrary collection of values.

Here's a more useful example: A method that returns a List of Customer objects. However, the method also returns an error code and a human-readable error message. Rather than defining a class, the code just declares a Tuple with an integer, a string, and List of Customers:

  Function ReturnData(Region As String) _
As Tuple(Of Integer, String, List(Of Customer))

Dim cust As Customer Dim lst As New List(Of Customer) Dim errorCode As Integer Dim errorMessage As String

'code to add customers to lst and catch errors

Dim tup As New Tuple(Of Integer, String, List(Of Customer)) (errorCode, errorMessage, lst) Return tup

End Function

The code that calls this method would use Item* properties on the Tuple to access the values. A Tuple, like this one, with three items stored in it will have properties called Item1, Item2, and Item3 (and all the properties will have the right data type).

To call the method, check for an error, and either display the error message or process the Customers in the list if everything works right, you'd write code like this:

  Dim retData As Tuple(Of Integer, String, List(Of Customer))
  
  retData = ReturnData("NA")
  
 If retData.Item2 < 0 Then
     MessageBox.Show(retData.Item2)
  Else
     For Each cust In retData.Item3
       'process Customers
     Next        
  End If

Editor's Note: This article has been updated to reflect that Tuples can hold up to eight values, not four.

Posted by Peter Vogel on 12/08/201114 comments


Getting Back to a Useful Method in Debug Mode

Sometimes, in debug mode, you find yourself sitting on a line in a method that you have no interest in. What you actually want to do is to get back out of the method and stop in some method you've already passed through. Of course, if you just want to get back to the method that called the code you've stopped in, you can use Step Out (Ctrl+Shift+F8). But if you're more than one level deep, getting back to someplace useful can be painful.

In a recent article on the debugging tools available in Visual Studio, I described the benefits of the Breakpoint window. What I didn't talk about was the Call Stack window which lets you stop in any method that you passed through on your way to the current line of code.

To use the Call Stack window, after you've stopped on a line, first bring up the window: either use the menu choice Debug | Windows | Call stack or use the distinctly un-intuitive Ctrl + L (in Visual Studio 2010).  That will give you a window with a list of methods executed on the way to the line you've stopped at.

The method you're stopped at appears right at the top of the window. Listed below that line are the methods you passed through on the way to this line of code. Now just right-click on one of those methods and select Run to Cursor (or select the method and then press Ctrl+F8). Your code will execute until control returns to the method you've selected in the Call Stack. Once you've stopped there, you can treat it like any other breakpoint.

If you want to set something more permanent, after selecting a method in the Call Stack, use F9 (or Debug | Toggle Breakpoint) to set a breakpoint on the method.

Posted by Peter Vogel on 11/29/20112 comments


Free Tool: Easily Test ASP.NET Pages in Different Browsers

Time was, I only worked on intranet projects because my clients could control the browser that users accessed the site with. This meant that the testing cycle for any project was relatively short: We only had to test the site with the "approved" browser. However, as browsers have become more compliant to standards—and as the economy has retracted—testing cycles for Internet applications have gotten shorter and I've started working on Internet projects. That change means that I need to be able to test my client's sites in multiple browsers. More

Posted by Peter Vogel on 11/16/20114 comments


Subscribe on YouTube