Login | Register   
LinkedIn
Google+
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
 

Our Secrets to the My Namespace in VB 2005 : Page 2

Productivity is one of the major goals of Visual Basic 2005 and with "My" Microsoft may just have hit a home run.


advertisement
Extending My Functionality
I build a lot of similar helper functions for my client's applications. It would be cool if I could somehow plug these helper functions or similar functions into the My namespace. My allows that through new functionality provided in the CLR. Through simple namespace manipulation you can add and extend items to My.

Let's look at how to add a completely new object under the My namespace. Suppose you have a global contact list and you want to build a class to provide quick access to your organization's information including office locations, phone numbers, etc. You would want your developers to access the office information with the following code:

For each office as Office in My.Organization.Offices OfficeList.Items.Add(office.Name) Next For

So how do you go about doing this? In the Community Tech Preview all you need to do is create a class in your project with a namespace of My such as the following:

Namespace My Public Class Organization Public Shared ReadOnly Property Offices() As Office() Get Dim workOffice() As Office = _ My.WebServices.Service.GetOffices() Return workOffice End Get End Property End Class End Namespace

Notice that I used My.WebServices to access the Web service I had referenced in my project. So instead of the following code:



Dim myWebServ As New CompanyInformation.Service Dim workOffices() As myWebServ.GetOffices

My.Webservices provides a nice little shortcut to all the Web services and also documents what Web services the application is using. I think this makes the code clearer because a developer will see that I am indeed calling a Web service and not just using an object.

Adding to My.Computer
My.Computer is a special case that allows you to add to the My.Computer object by using partial classes. Partial classes, a new feature in VB8, allow you to combine multiple files into one class. (A deep discussion of partial classes is outside of the scope of this article.) Suppose your application is for PCs with integrated phones. You could add the phone to My.Computer with code similar to the following:

Namespace My Partial Friend Class MyComputer Private _phone As Phone Public ReadOnly Property Phone() As Phone Get If _phone Is Nothing Then _phone = New Phone End If Return _phone End Get End Property End Class Public Class Phone Friend Sub New() End Sub Public Sub Dial(ByVal number As String) 'Use Telephony API(TAPI) stuff here End Sub Public Sub Answer() 'Use Telephony API(TAPI) stuff here End Sub End Class End Namespace

My Introduces Application-Level Events for Visual Basic
VB8 brings application-level events to Windows Forms—a concept that ASP and ASP.NET applications have had for years. In my opinion this concept is even better suited for Windows Forms. In a .NET 2.0 Windows Forms application you have access to the four following events:

  • Startup—This event fires when the application first starts.
  • Shutdown—This event fires when the application shuts down.
  • StartupNextInstance—This event fires when a new instance of the application starts.
  • UnhandledException—An event that lets you gracefully shut down an application if an exception occurs that is not handled. (This was possible in VB7.1).
You can also use these application-level events from C# if you reference the Microsoft.Visual Basic assembly.

I think that Microsoft could make using My to find the current user of the application more clear.
Let's look at how the UnhandledException event clears up some VB7.1 code. In VB7.1 you could handle an unhandled exception with the following code:

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyHandler Private Sub MyHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs) 'Do something useful here End Sub

As you can imagine, VB8 introduces a more intuitive implementation.

Private Sub MyApplication_UnhandledException( ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs) Handles Me.UnhandledException 'Do something useful here End Sub

Of course you should do something useful when your code triggers this event.

My Adapts to the Project Type
You will notice, depending on the type of project you are working on, that My will not behave exactly the same because it is project sensitive. For example, in an ASP.NET application My will not provide information that does not make sense in that runtime environment. This holds true for Windows Forms applications, Console applications, etc.

My is not a canned library that tries to cover all the different project types available in the .NET Framework. Depending on the project you are working in, Intellisense will show you different classes within My. For instance, in a Windows Forms project you will see: My.Application, My.Computer, My.User, My.Resources, and My.Settings

An ASP.NET project exposes the following classes:

  • My.Application
  • My.Computer
  • My.User
The classes offer different information. For example, in an ASP.NET project My.Application exposes Web-related items including Session, Request, Response, etc. In a Windows Forms project My.Application exposes information related to items such as the MainForm, the OpenForms collection, etc.

Visual Basic 2005 (the tools and the language) puts Visual Basic clearly in the lead when it comes to producing clean, maintainable, robust code in a rapid fashion. My and the new capabilities of the Base Class Libraries that Microsoft build to support My is just an example of this robustness that .NET developers using Visual Basic will come to enjoy.



Keith Franklin is the Chief Software Architect of a leading .NET consulting firm in Chicago. Keith is a Microsoft MVP, a regular speaker at VSLive!, DevDays, Microsoft launch events, and the Chicago .NET Users Group, which he founded and directs. His close relationship with members of the Microsoft .NET teams, publishers, speakers, and user group community led to his appointment to the Board of Directors of the International .NET Association (INETA) in September 2001. He authored one of the first Visual Basic books devoted to the .NET Framework. He has also authored numerous articles published by leading .NET and Visual Basic publications and is a member of the .NET Developers Journal editorial board.
Comment and Contribute

 

 

 

 

 


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

 

 

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