Generics is a new feature in the .NET 2.0 Framework. As such, languages such as C# and Visual Basic 2005 now support this new feature.
Using Generics, you can define a class definition without fixing the data type that it is using at design time. To better understand how Generics work, consider the case of a Queue class. A queue is a first-in-first-out data structure and is useful for storing messages in the order they were received for sequential processing.
shows that the MyQueue class defines a circular queue data structure that allows items to be enqueued (inserted) and dequeued (removed).
What is notable in this class is that I have not fixed the specific data type of the items that can be inserted into the queue. Rather, I have used the Of
keyword as a placeholder for the actual data type to be used.
Public Class MyQueue(Of itemType)
To use the MyQueue class with Integer values, simply instantiate the MyQueue class with Of Integer
(in this case, q1
is known as a constructed type).
Dim q1 As New MyQueue(Of Integer)(3)
MsgBox(q1.Dequeue) ' prints 5
MsgBox(q1.Dequeue) ' prints 6
MsgBox(q1.Dequeue) ' prints 7
q1.Enqueue("A string") ' error
If you want to use the MyQueue class with string values, then use Of String
as the parameter.
Dim q2 As New MyQueue(Of String)(3)
q2.Enqueue("VB 2005 ")
MsgBox(q2.Dequeue) 'prints "Microsoft"
MsgBox(q2.Dequeue) 'prints "VB 2005 "
MsgBox(q2.Dequeue) 'prints "rocks!"
q2.Enqueue(5) ' error
The advantage of using generics for this example is that I do not need separate classes for different types of data types. Of course, one way would be to use an Object data type for the items inserted into the queue and during runtime perform type casting to get the required data type. However, doing so results in late binding and affects performance and type-safety.
|Figure 4. Code Snippets: Inserting code snippets is easy.|
To summarize, generics affords the following:
- Type Safety. Generic types enforce type compliance at compile-time, and not run-time (as in the case of using Object). This reduces the chances of data-type conflict during run time.
- Performance. The data types to be used in a Generic class are determined at compile time, hence there is no need to perform type casting during run time, which is a computationally costly process.
- Code Reuse. Since you only need to write the class once and customize it to use with the various data types, there is a substantial amount of code-reuse.
In an attempt to make Visual Basic more accessible to a wider developer base, Microsoft has made a very innovative addition to Visual Studio 2005code snippets
Code snippets are small, commonly-used blocks of code. Code snippets allow you to insert commonly-used code blocks into your project, thereby improving the efficiency of your development process. This is very useful for developers who are new to the language and need a way to jumpstart their development.
|Figure 5. Inserting Code Snippets: The figure shows the process of Inserting a code snippet to encrypt a string.|
In the final release of Visual Studio 2005, it is expected that Microsoft will ship more than 500 code snippets, but in the current release (Beta 1 at the time of writing) there are only about 30 or so code snippets.
To insert a code snippet into your code, simply right-click in Code View and select "Insert Snippet..." (see Figure 4
You can then choose the code category that suits your purpose. For example, if you choose Security, you will see a listing like that shown in Figure 5
|Figure 6. Inserted Code Snippet: The inserted code snippet with the placeholder.|
If you choose "Decrypt a String," a block of code would then be inserted into your application. To customize the code, simply replace the placeholder highlighted in yellow (see Figure 6
Code snippets are stored in the C:\Program Files\Microsoft Visual Studio 8\Vb\Snippets\1033
folder. They are stored as XML files so you can write your own code snippets to be shared among your colleagues.