Tuesday, August 7, 2012

Grouping Partial Classes in Visual Studio Solution Explorer

Much has already been said on how useful .Net's partial classes can be. But not much on how to organize them well in Visual Studio's Solution Explorer. Given the below web application project:

Figure 1: New Partial Class File Added

The aspx page WebForm1.aspx has the default code-behind file and the designer file. For example, let's add a partial class to WebForm1 to handle validations for the page. By default, this new class file is not grouped with WebForm1.aspx like the others. Wouldn't it make sense to group this new partial class with the existing group?

The extension VSCommands 2010 has a handy command to join the new class into the existing group. It is as simple as selecting the parent class (in this case, the aspx file) and the new partial class (the aspx.validations.cs file), then clicking on Group Items.

Figure 2: Using VSCommands to Group Items

From here, a popup window will appear verifying which one should be the parent of the selected files, as shown below for Visual Studio 2010:

Figure 3: VSCommands Root Item Selector

We'll keep the aspx file as the parent. With this, we now have the following structure in the Solution Explorer:

Figure 4: New Partial Class Added to Group

The new partial class aspx.validations.cs is now a member of the WebForm1 class group! This also works for Windows Forms projects, and Visual Basic projects as well.

It should be noted that this makes sense for partial classes, but we are not limited to partial classes when grouping them in Solution Explorer! We can join any other class into the group if we wanted. Say, if we have a dedicated helper class for WebForm1 and it is not a partial class, we can still add it to the group. And we can select more than two files at the same time to initiate the item grouping. There will just be more files listed in VSCommands' root item selector in figure 3. It is even possible to attach a class to another sub-item in an existing group, creating deeper levels in the solution tree.

We actually don't need VSCommands to take advantage of this. The grouping is configured in the project file, as shown in the below snippet:

Figure 6: Project File Configurations
In figure 5, the new partial class aspx.validations.cs is given a new node value denoting which existing object it should be attached to. The default classes already has this node connected to WebForm1.aspx, so it is grouped by default. VSCommands just makes things easier and safer to do.

Sunday, April 1, 2012

Mixed Data Types in .NET Arrays

The first thing we learn about arrays is that we declare them as an array of a specific type. Consider the following examples:

The data type for the elements of an array variable is explicitly determined during design-time. Any attempt to assign a string in boolArray element will result in a compile error. To be able to have an array with multiple data types for the elements, we can use ArrayLists. But we can use the basic arrays to store different data types in the elements. We simply declare the variable as an array of objects.

Running this as a console application gives us the below output...


... showing that the elements are indeed stored as the assigned values' types.

Of course, just because this is possible with arrays doesn't mean we should be doing it just because. Declaring variables as objects should be avoided whenever possible. But there is a place for object arrays in the right circumstances.

To quote another blog who described it really well, "...an ArrayList is simply a collection class - one that supports the IList interface - whose internal memory structure is an array. The keyword here, however, is internal. The ArrayList is not an array." [1]

This means that if we know the size of the array ahead of time, we're better off using object arrays than an ArrayList to avoid the overhead of the internal implementations of the IList interface in the latter. Also, since internally it is an array, adding elements into an ArrayList also involves creating a new array with additional elements, and then copying the old array elements into it.

Just a few things worth considering during design.

[1] Array vs ArrayList by Lycangeek