Introduction
Here are my learnings on My namespace. It's not much but here you go before you ask questions about "how do I handle events outside of the my namespace definitions inside my code?!?!"
Using the Code
I will present uxeHandler, an unhandled exception handler. It uses My namespace hooking to allow for instant exception handling.
The My namespace is a great thing to use. It allows for globalizing data (and gives new classes for global data access.) An example of this is the UnhandledException event in My.MyApplication. It is raised when an unhandled exception is thrown.
You could write inline code handling like this:
try
throw new exception("RandomNumberException")
catch e as exception
messagebox.show(e.tostring())
end try
But this is only useful when you're expecting a certain exception. It does NOTHING when you have no idea what's coming your way. Try this on for size though:
try
dim imgdbconn as new imageDBConnection("localhost", 3000)
dim images as new system.collections.ObjectModel.collection(of Image)
dim cindex as integer = 0
for each cindex in imgdbconn.getdb(0).items
images.add(cindex.image)
end for
catch e as exception
if(e.message = ...) Then
...
ElseIf(...)
ElseIf(...)
Finally
imgdbconn.closeConn()
End Try
That's a lot. There you'd use a lot of things -- nesting trys. But what happens if you get something else? Something that you can't handle? Something completely out of the range of what could happen. This fires the UnhandledException method. But wait a second, that's in My! I can't touch that!
Well, it was thought that you couldn't touch the My namespace. But you can.
That takes me to uxeHandler. First, we don't want people to randomly call this. Nor do we want to have a show() mechanism available. ShowDialog() may be, but not show(), as that allows for interaction with the application while the system is trying to handle the exception. ShowDialog will disallow interaction with the parent form.
Here's the source for UxeHandler:
Imports System.Windows.Forms
Imports Microsoft.VisualBasic.ApplicationServices
Public Class uheHandlerWindow
Private Shadows Sub show()
Me.InitializeComponent()
End Sub
Public Sub New(ByVal e As
Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
Me.InitializeComponent()
Me.Text = My.Application.Info.AssemblyName
Me.message.Text = e.Exception.Message
Me.payload.Text = e.Exception.ToString()
Me.payload.Text += vbNewLine + vbNewLine
End Sub
Private Sub OK_Button_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles OK_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Cancel_Button.Click
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
End Class
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(ByVal sender As Object,
ByVal e As
Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles
Me.UnhandledException
If (New uheHandlerWindow(e).ShowDialog() = DialogResult.OK) Then
e.ExitApplication = False
Else
e.ExitApplication = True
End If
End Sub
End Class
End Namespace
Here we see the guts of it all. Payload is a TextBox to show what has happened and message is a label to tell the user what happened. But then we see something down at the bottom. It's a My Namespace definition!
You can add My namespace values easily by simply using a My namespace outside of the class definition.
Example:
Class myblahblah
public sub new(...)
...
End Sub
End Class
Namespace My
Partial Freind Class MyBlahs
Private Sub getBlahs(...)
...
End Sub
End Class
End Namespace
This will create a new set of functions that allows for the calling of My.MyBlahs.getBlahs(...) throughout the project by simply adding the one file to the project.
How It Works
The compiler looks for ways to streamline the code. This includes looking for partial friend classes that it can glom together. Partial Friend classes can be spanned across multiple files this way.
Points of Interest
Originally, I wrote this for an internal project, but wanted to share it here. I originally was using a big MessageBox and drawing controls over it (but then found the designer to be well rounded. :) Also, you'll notice there's a show() function -- this is only for the .NET Framework to handle some basics.
History
Some basic history goes here:
- Rev 1 of article December 11, 2007 rev 2 of
uxeHandler
- Rev 2 of article December 13, 2007