Click here to Skip to main content
Licence CPOL
First Posted 11 Dec 2007
Views 7,173
Downloads 64
Bookmarked 7 times

Hooking into My Namespace

By | 13 Dec 2007 | Article
Describes and gives 2 examples of using a hook into My namespace to add functionality

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 and throw something.

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 loading in all the images and data

try
    dim imgdbconn as new imageDBConnection("localhost", 3000) ' initialize the local db

    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

''' <summary> 
''' uxeHandlerWindow is a drop-in dialog for exception handling.
''' Created by Unknown Software for internal use, it is now being placed under
''' Creative Commons Share Alike Attribution.
''' </summary>
''' <remarks>
''' This should be drop in: add to your VB.NET project and you get instant
''' unhandled exception handling. IF NOT then paste the sub into your 
''' MyApplication\ApplicationEvents area. 
'''
'''</remarks>

Public Class uheHandlerWindow
    Private Shadows Sub show()
        Me.InitializeComponent()
    End Sub
    Public Sub New(ByVal e As 
        Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
        ' -- required, initializes the component database and makes the layout
        '    occur nicely.

        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

' this Is to allow for drop-in exception handling. simply paste this into your
' ApplicationEvents if you don't want it handled here.

Namespace My
    Partial Friend Class MyApplication
        ' MyApplication event handling.
        ' UnhandledException: Raised if the application encounters an unhandled exception.
        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:

  1. Rev 1 of article December 11, 2007 rev 2 of uxeHandler
  2. Rev 2 of article December 13, 2007

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Indrora

Software Developer
Unknown Software
United States United States

Member

I have been writing code since i was 8, working in SiMPLE and then progressing into C++ and VB.NET. I take passion in making simple tools that make life easier.
 
Unknown software was a tag made up by me at age 14 for the demo of Visual studio 5 so I could get the betas.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 13 Dec 2007
Article Copyright 2007 by Indrora
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid