Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Use Generics to Create an Audit Trail : Page 2

Building an audit trail into your application provides a mechanism for tracking who updated what when, and the new generics feature in Whidbey will help you build that trail.

Building a Generic List
When your application needs to retain a set of data or objects, you can use one of the many collection-type classes provided in the .NET Framework such as Stack, Queue, and Dictionary. These classes allow you to store any type of object in the collection. In some cases this may be a good thing, such as when you want to keep a list of the last set of changed objects. In many cases, however, you want to ensure that a collection only contains objects of a particular type.

Generics provide a way to build general code that is specific at run time.
In Visual Studio .NET 2003 you can only create a strongly-typed collection class by building a collection class for each type. For example, if you want a strongly-typed collection for strings and a strongly-typed collection for integers, you would create two collection classes, one for strings and one for integers. This makes it laborious to create strongly-typed collections for many different types.

With Whidbey and generics, you can create a single collection class and define its type at run time, just like in the prior structure.

In Visual Basic .NET "Whidbey":

Public Class List(Of ItemType) Private elements() As ItemType End Class

In C# "Whidbey":

public class List<T> { private T[] elements; }

The Visual Basic .NET example defines a List class with an elements array that will only hold items of a specific type, generically represented in the example as ItemType. Likewise, the C# example defines a List class with an elements array that will only hold items of a specific type, generically represented in the example as T. In both examples, you can use any variable name to represent the generic variables shown as ItemType and T.

When you use the List class, you define the desired data type for the list.

In Visual Basic .NET "Whidbey":

Dim intList as New List(Of Integer) Dim strList as New List(Of String)

In C# .NET "Whidbey":

List<int> intList = new List<int>(); List<string> strList = new List<string>();

The first line of each example creates a list of integers and the second line creates a list of strings.

You can then add code to the List class to manage the list. Every List class needs a count of the number of elements, a way to add elements, and a way to get and set element contents.

In Visual Basic .NET "Whidbey":

Private iIndex As Integer = -1 Public Sub Add(ByVal element As ItemType) ' Start the array out at 10 If iIndex = -1 Then ReDim elements(10) End If If (iIndex = elements.Length) Then ReDim Preserve elements(iIndex + 10) End If iIndex += 1 elements(iIndex) = element End Sub Default Public Property _ Item(ByVal index _ As Integer) As ItemType Get Return elements(index) End Get Set(ByVal Value As ItemType) elements(index) = Value End Set End Property Public ReadOnly Property Count() As Integer Get ' The index starts at 0, the count at 1 ' So adjust the index to the count Return iIndex + 1 End Get End Property

In C# .NET "Whidbey":

private int count = -1; public void Add(T element) { if (count == -1) { // Start with 10 elements elements = new T[10]; } // Increment by 10 more as needed if (count == elements.Length) { elements = new T[count + 10]; } elements[++count] = element; } public T this[int index] { get { return elements[index]; } set { elements[index] = value; } } public int Count { get { return count; } }

You can then add items and retrieve their contents in a strongly-typed manner. This means that value types, such as integers, won't be boxed and won't require casting. It also means that no one can accidentally put anything but an item of the defined type into the list.

You can use the List class to manage a list of integers:

In Visual Basic .NET "Whidbey":

Dim intList as New List(Of Integer) intList.Add(1) ' No boxing intList.Add(2) ' No boxing intList.Add("Three") ' Compile-time error Dim i As Int32 = intList(0) ' No cast required For j As Int32 = 0 To intList.Count Debug.WriteLine(intList(j).ToString) Next

In C# .NET "Whidbey":

List<int> intList = new List<int>(); intList.Add(1); // No boxing intList.Add(2); // No boxing //intList.Add("Three"); // Compile-time error int i = intList[0]; // No cast required for (int j = 0; j <=intList.Count; j++) { Debug.WriteLine(intList[j]); }

Since the List class is generic, you can also use it to manage a collection of strings:

In Visual Basic .NET "Whidbey":

Dim strList as New List(Of String) strList.Add("This") strList.Add("tests") strList.Add("generics") For j As Int32 = 0 To strList.Count Debug.WriteLine(strList(j)) ' No cast required Next

In C# .NET "Whidbey":

List<string> strList = new List<string>(); strList.Add("This"); strList.Add("tests"); strList.Add("generics"); for (int j = 0; j<=strList.Count; j++) { Debug.WriteLine(strList[j]); // No cast required }

Using generics you can create one set of code that works with any data type and yet is strongly-typed at run time. And, since there are no boxing or casting operations, you also get improved performance.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date