Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / Windows Forms
Article

VB 2005: Some Cool New Features

Rate me:
Please Sign up or sign in to vote.
4.62/5 (18 votes)
5 May 200511 min read 177.3K   56   13
A look at some cool new VB 2005/.NET 2.0 features

Introduction

Some very interesting new features are available within the VB 2005 development environment. Here are just a few of them that I’ve made note of while trying to familiarize myself with this much improved IDE.

Note: I'm working with VS 2005 Beta 2, so everything is subject to change.

Design Time Configuration of Single Instance Applications

VB 2005 Windows based applications can optionally be set to be single instance applications simply by setting an option within the ‘Application’ section of the project’s properties tab:

(Beginner’s Note: To access a project’s properties tab, choose from the main menu ‘Project->[Project Name] Properties’)

(screenshot 1)

Image 1

Once the ‘Make single instance application’ option is checked, any attempt to spawn another instance of the application will result in the activation of the original instance. Also, the original instance will be notified of the attempt to spawn the additional instance via the application’s StartupNextInstance event. To write code for this event, simply:

  1. Click on the ‘View Application Events’ button (see screenshot 1 above). This will take you to the application’s code module.
  2. From within the code module window, choose ‘(MyApplication Events)’ from the drop down list located on the upper left hand corner.
  3. From within the code module window, choose ‘StartupNextInstance’ from the drop down list located on the upper right hand corner.
  4. Write the code to be executed within the automatically generated event handler.

The result should look something like:

VB
Imports Microsoft.VisualBasic.Devices
Imports Microsoft.VisualBasic.ApplicationServices

Namespace My

  Class MyApplication        

    'StartupNextInstance: Raised when launching a single-instance 
    'application and the application is already active. 
    Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, _
          ByVal e As StartupNextInstanceEventArgs) _
          Handles Me.StartupNextInstance

      MessageBox.Show("You're asking for trouble")

    End Sub       

  End Class

End Namespace

Prior to the availability of this design time feature, programmers were forced to write their own logic for enabling single instance applications or do what I did and use Manish K. Agarwal's beautiful piece of code available right here on CP. Regardless, code had to be written.

Note that the availability of all ‘Windows application framework properties’ (see screenshot 1 above) requires that the ‘Enable application framework’ option (see screenshot 1 above) be checked. Otherwise, all of these features, including the ‘Make single instance application’ property, become disabled and the developer will have to accomplish all corresponding tasks by hand. Furthermore, if this option is checked, the ‘Startup form’ setting must be set to a form within the project; that is, it cannot be set to a Main procedure entry point. Not a problem though since the application framework exposes a series of events that you can declaratively handle to accomplish the same functionality but in a much more elegant manner (IMHO). These events are:

VB
Imports Microsoft.VisualBasic.Devices
Imports Microsoft.VisualBasic.ApplicationServices

Namespace My

  Class MyApplication

    'NetworkAvailabilityChanged: Raised when the network 
    'connection is connected or disconnected.
    Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object,_
          ByVal e As NetworkAvailableEventArgs) _
          Handles Me.NetworkAvailabilityChanged
    End Sub

    'Shutdown: Raised after all application forms are closed.  
    'This event is not raised if the application is terminating abnormally.
    Private Sub MyApplication_Shutdown(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Me.Shutdown
    End Sub

    'Startup: Raised when the application starts, 
    'before the startup form is created.
    Private Sub MyApplication_Startup(ByVal sender As Object, _
              ByVal e As StartupEventArgs) Handles Me.Startup
    End Sub

    'StartupNextInstance: Raised when launching a single-instance 
    'application and the application is already active. 
    Private Sub MyApplication_StartupNextInstance(ByVal sender As Object,_
              ByVal e As StartupNextInstanceEventArgs) _
              Handles Me.StartupNextInstance
    End Sub

    'NetworkAvailabilityChanged: Raised when the network 
    'connection is connected or disconnected.
    Private Sub MyApplication_UnhandledException(ByVal sender As Object, _ 
              ByVal e As UnhandledExceptionEventArgs) _
              Handles Me.UnhandledException
    End Sub

  End Class

End Namespace

Design Time Configuration of Splash Screens

It is no longer necessary for programmers to write code that will display a splash screen prior to the loading of the application’s main form. At design time, a Windows application framework property can be set that will automatically

  1. display a splash screen of the programmer’s choice before and while the main form loads.
  2. unload the splash screen as soon as the main form is displayed.

To set up a splash screen:

  1. Open up the project’s properties tab.
  2. From within the ‘Application’ section of the project’s properties tab (see screenshot 1 above), select the form you would like to show as a splash screen from the ‘Splash screen’ drop down list (see screenshot 1 above). This list will contain all the forms currently available in your project. Note that VB provides a generic splash screen template which you can add to your project and use as the splash screen with or without modification. To add this generic template, simply choose from the main menu ‘Project->Add New Item’ and then select the ‘Splash Screen’ item.
  3. Once the splash screen has been selected (see screenshot 1 above), run the application and notice that the splash screen appears before the main form is displayed and immediately disappears afterwards. Of course, the amount of time the splash screen is displayed is dependent on the amount of time your main form takes to display. If your main form is displayed instantly, then of course there’s no need for a splash screen.

Here’s what the generic splash screen template looks like (pretty darn cool IMHO):

(screenshot 2)

Image 2

Compiler Feedback Control

Some very cool code focused features have been added to the VB 2005 compiler. Take a look at the ‘Compile’ section of a VB 2005 project’s properties tab:

(screenshot 3)

Image 3

As is always the case with VB, the programmer is always in control of the compiler, not the other way around, such that the above compiler features can be configured such that when one of the above conditions is encountered within the code one of the following things happens.

  1. Nothing happens.
  2. A warning is displayed.
  3. A compile time error takes place.

Most of these compiler features have been copied from the awesome C# compiler, and indeed they are very welcomed to say the least.

Also, another very cool and much needed new feature that has been copied from the C# IDE is the ability to handle build events so that certain things happen before and/or after your project is compiled. If you click on the ‘Build Events’ button (see screenshot 3 above) you will get the following screen:

(screenshot 4)

Image 4

Now it’s easy, for example, to copy the build output to various locations other than the build output path, perhaps to a folder from which an install package obtains its files from. Or perhaps maybe you need to inject some IL code to the build output in order to implement some aspect. Whatever it is, here (see screenshot 4 above) you can do it.

Out of the Box FxCop Integration

VB 2005 comes equipped with the same code analysis tools offered by FxCop in order to facilitate, for example, the process of making sure your code is CLS compliant. To configure code analysis:

  1. Windows Applications: open up the ‘Code Analysis’ section of the project’s properties tab.
  2. Web Applications: open up the ‘Code Analysis Configuration’ dialog accessible via the main menu (‘Website->Code Analysis Configuration’ ).

The result should be something like:

(screenshot 5)

Image 5

Here, you can make all types of configurations such that when you run the code analysis, only those rules you have checked will display either warnings or errors, depending on your configurations, in the event they are broken. To run code analysis:

  1. Windows Applications: from the main menu choose ‘Build->Run Code Analysis on [Project Name]’.
  2. Web Applications: from the main menu choose ‘Build->Analyze Website’.

It’s fantastic that now the IDE itself informs me of my “bad” coding habits, although quite honestly CLS compliancy, for example, is usually the last thing I worry about, especially during RAD development, and, therefore, I prefer to leave all rules at the warning level. Nonetheless, having FxCop integration and customization directly within the development environment is awesome (IMHO)!

Note that in order for the code analysis feature to work, the ‘Enable Code Analysis’ option (see screenshot 5 above) needs to be checked, this not being the default behavior.

Some Very Cool New Language Extensions

VB 2005 has several new language extensions. The ones that everyone knows and talks about are those that enable VB 2005 to consume and produce Generics, for example. Also, Operator Overloading, the consumption and production of it, is of course another big one that is now a part of the VB grammar. However, there are many other smaller language extensions that have really made me jump up and down on top of my desk. For example:

Using Statement

The Using statement was copied from C# and is used to facilitate implementation of the Dispose pattern. With this statement programmers can be assured that IDisposable types wrapped within this statement are always cleaned up without the need to wrap these objects within exception handlers and explicitly invoke their Dispose methods. Most of you should know what I’m talking about here, but if you’re not completely sure, see Daniel Turini’s Exception Handling Best Practices in .NET.

Check out the following dummy piece of code:

VB
Using cn As New OleDbConnection("mycnnstring"), _
           cmd As New OleDbCommand("mycmdtext", cn)
  cn.Open()
  Using rdr As OleDbDataReader = _
     cmd.ExecuteReader(Data.CommandBehavior.SingleResult)
    While rdr.Read()

    End While
  End Using
End Using

Here we have two Using statements, one nested within the other. The first one:

  1. acquires upon entry, a database connection object and a database command object.
  2. uses throughout the code block the objects acquired in step 1.
  3. disposes on exit of the code block, whether or not by exception, the objects used in step 2, thereby, freeing developers from having to explicitly dispose of these resources.

The nested Using statement does the same thing as the outer Using statement, only that it acquires, uses, and disposes off a database reader object.

Continue Statement

It’s about darn time! Finally VB has a Continue statement that can be used to jump to the next iteration of a loop rather than having to wrap loop logic within conditional statements. Furthermore, it’s also possible to specify which loop is to be jumped to! Cool! Indeed the wait has been worth it.

Check out the following piece of dummy code:

VB
For i As Integer = 0 To 10
    While True
        If someCondition Then
            Continue While
        ElseIf someOtherCondition Then
            Continue For
        End If
        Do
            If someCondition Then
                Continue Do
            ElseIf someOtherCondition Then
                Continue While
            Else
                Continue For
            End If
        Loop While someOtherCondition
    End While
    If someCondition Then
        Continue For
    End If
Next

Here we have three loops, a For loop, a While loop nested within the For loop, and a Do loop nested within the While loop.

  1. Within the For loop we jump (Continue For) to the next iteration of the same For loop if some condition is met.
  2. Within the While loop we jump (Continue While) to the next iteration of the same While loop if some condition is met. If some other condition is met we jump (Continue For) to the next iteration of the outer For loop.
  3. Within the Do loop we jump (Continue Do) to the next iteration of the same Do loop if some condition is met. If some other condition is met we jump (Continue While) to the next iteration of the outer While loop. Otherwise, we jump (Continue For) to the next iteration of the outer While loop’s outer For loop (I’m getting tired).

Now is that cool or is that awesome! In the words of Keanu Reeves: Wow!

Mixed Access Modifiers for Property Getters and Setters

This component based programming feature was taken away from VB in its transition from COM to .NET but now its back, and thank goodness for that! With this feature we can once again define properties within our classes that, for example, have public read access, so that types outside the assembly can read them, and internal write access, so that only types within the same assembly can write to them.

Check out the following piece of dummy code that illustrates the scenario just mentioned:

VB
Private _myProp As String

Public Property MyProp() As String
    Get
        Return _myProp
    End Get
    Friend Set(ByVal value As String)
        _myProp = value
    End Set
End Property

The above property, MyProp, can be read by all clients, even those originating outside the containing assembly. However, the property can only have its value set by types originating inside the containing assembly.

Note that either the getter or the setter, but not both, can have a different access modifier than the property itself. Also, the access modifier specified for the getter or the setter must have a lower accessibility level than the property itself, so, for example, if you have a Protected property, then the access modifier, if any, specified for either the setter or the getter must be Private, since any other access modifier has a less restrictive accessibility level than the Protected one.

There's So Much More

There are a way too many new things that have popped up within the VB 2005 IDE for me to cover here, some features applicable only to VB, others to the entire .NET 2.0 Framework. Do yourself a favor when you get the time and start playing around with VS 2005 Beta 2. It's awesome! Did I mention all new web and Windows controls that come straight out of the box? Well, maybe next time, assuming there is one.

Final Concluding Thoughts

VB 2005 Rocks!

Seriously, I have just touched the surface of the new language and RAD features VB 2005 puts on the table, not to mention all of the new and improved features of .NET itself. I highly recommend anyone interested in VB development, whether professional programmers or hobbyists, to get a copy of VS 2005 Beta 2 and start playing around. Also, make sure to take a look at the help that comes along with it, which is actually pretty decent given the breadth of the product. So far, it promises to be a great tool for all types of development, whether trivial or advanced.

For those of you who are still developing with VB6, particularly the hobbyists, be assured that the .NET transition will be much easier once VB 2005 arrives, mainly because of the magnificent My facade, a topic way beyond the scope of this writing. Again, get a copy of VS 2005 beta 2 and start playing around with it. Do not be afraid! Don’t let the loud noise about all the VB language changes discourage you from using the tool. Indeed the language has changed significantly but that certainly does not mean that you have to use or even know about these changes, rather let the professional programmers worry about these changes. I guarantee you that using VB 2005 is a whole lot easier than using VB6. The Intellisense and code editor features are exponentially better so that you barely have to type anything, the data access RAD features are so incredibly productive that you can create powerful database applications with hardly any hand generated code, and the list goes on and on. Again, do not be afraid and, furthermore, don’t even worry about what a class is, because you do not need to know object oriented programming to build applications with VB, more so the case with VB 2005. Just do it! Your development chores will become much easier to do once you make the switch!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI can't see any of these features!! Pin
Hakkoos12-Apr-07 5:22
Hakkoos12-Apr-07 5:22 
GeneralA little help troubleshooting... Pin
VB_n00b20-Oct-05 15:13
sussVB_n00b20-Oct-05 15:13 
Generalaccess modifiers Pin
candy1125-Aug-05 7:54
susscandy1125-Aug-05 7:54 
QuestionHow to Seach in Database... Pin
Gul Raiz15-May-05 9:56
Gul Raiz15-May-05 9:56 
QuestionChange is good. Or is it?? Pin
TVW-Dev10-May-05 6:39
TVW-Dev10-May-05 6:39 
AnswerRe: Change is good. Or is it?? Pin
Giancarlo Aguilera10-May-05 7:30
Giancarlo Aguilera10-May-05 7:30 
GeneralRe: Change is good. Or is it?? Pin
TVW-Dev10-May-05 8:27
TVW-Dev10-May-05 8:27 
well I am glad you responded. I am also glad you code with C# as well, as I mentioned before so do I. I guess my whole qualm is not with the benefits with .Net 2.0 as you so intuitively pointed out. But with the IDE and the way it’s put together for VB. the IDE is there to make us more efficient and to let the average Joe make a GUI or whatever they want "QUICKLY". I feel that 2003 does this very well for VB and the new features and operation of the 2005 VB IDE does not seem to benefit ME at all. Believe me I have tried and tried to like it. Years ago I worked my butt off to learn OO when I switched from vb6 and learned vb.net & C#.net and became a better programmer for it. I don’t feel comfortable with this new program even after 2 months of playing with it. don’t think I am not up to the challenge; Try learning Xcode and coding in Cocoa for OSX (my first venture into coding for another platform). These of course are my personal views, and I put them out there for folks like you to tell me why you like it. Maybe I will warm up to it. Shoot, maybe I will have to if I want to keep using VB. OR maybe I am just turning into a curmudgeon! Smile | :)
Questioneh? Pin
Dead Skin Mask5-May-05 22:57
Dead Skin Mask5-May-05 22:57 
AnswerRe: eh? Pin
Giancarlo Aguilera6-May-05 4:50
Giancarlo Aguilera6-May-05 4:50 
GeneralRe: eh? Pin
Dead Skin Mask6-May-05 5:16
Dead Skin Mask6-May-05 5:16 
GeneralRe: eh? Pin
Giancarlo Aguilera6-May-05 5:23
Giancarlo Aguilera6-May-05 5:23 
GeneralVB 2005 Rocks! Pin
NormDroid5-May-05 22:04
professionalNormDroid5-May-05 22:04 
GeneralRe: VB 2005 Rocks! Pin
Giancarlo Aguilera6-May-05 7:10
Giancarlo Aguilera6-May-05 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.