Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

What's New in Visual Basic 2005? : Page 3

Going forward, Microsoft will make a big push to migrate Visual Basic 6 developers to Visual Studio 2005. The Visual Basic team has addressed many of the areas that caused VB6 developers to hold on to their old code base.


advertisement
Generics
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.

Listing 3 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) q1.Enqueue(5) q1.Enqueue(6) q1.Enqueue(7) 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("Microsoft ") q2.Enqueue("VB 2005 ") q2.Enqueue("rocks!") 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.
Code Snippets
In an attempt to make Visual Basic more accessible to a wider developer base, Microsoft has made a very innovative addition to Visual Studio 2005—code 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.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap