Results 1 to 5 of 5

Thread: Customizing My.Namespace

Threaded View

  1. #1

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Customizing My.Namespace

    The following focuses on one idea for reducing the amount of code you type, centralizing code used often in development of solutions and to expose functionality within the .NET Framework that is rarely used but when needed, is needed now.

    One of the most common thing we do with users is to communicate with them using dialogs such as MsgBox which works great for most of the time yet can fall short say if you need ask a question and want the NO button as the default button which means you need the MessageBox class. The problem with the MessageBox is that by the time you are finished constructing a dialog it is a long line of code and you need to repeat it over and over again or place the code in a module and go from there.

    One possible method to assist in the above is to extend My Namespace.

    MSDN http://msdn.microsoft.com/en-us/library/bb531245.aspx
    The My namespace in Visual Basic exposes properties and methods that enable you to easily take advantage of the power of the .NET Framework. The My namespace simplifies common programming problems, often reducing a difficult task to a single line of code. Additionally, the My namespace is fully extensible so that you can customize the behavior of My and add new services to its hierarchy to adapt to specific application needs. This topic discusses both how to customize existing members of the My namespace and how to add your own custom classes to the My namespace.

    So instead of the following
    Code:
    If Windows.Forms.MessageBox.Show("Your question", "Question", _
                                     Windows.Forms.MessageBoxButtons.YesNo, _
                                     Windows.Forms.MessageBoxIcon.Question, _
                                     MessageBoxDefaultButton.Button2) = _
                                     Windows.Forms.DialogResult.Yes Then
    End If
    By extending My Namespace we could have this.
    Code:
    If My.Dialogs.Question("Your question", "question") Then
    
    End If
    Another tedious message to display is when an exception has to be displayed to users (personally I have a class which handles unhandled exceptions coupled with a replacement for globally handling unhandled exceptions). Take the following example which shows a personal message coupled with the text from the exception.
    Code:
    Try
       DirectCast(sender, ListBox).Items.Add("Ha Ha")
    Catch ex As Exception
       MsgBox(String.Format("I goofed up{0}{1}", _
                            Environment.NewLine, ex.Message))
    End Try
    Again using an extension to My namespace we could wrap this up and also format the dialog as shown below.
    Code:
    Try
       DirectCast(sender, ListBox).Items.Add("Ha Ha")
    Catch ex As Exception
       My.Dialogs.ExceptionDialog("I goofed up", "Oooops", ex)
    End Try
    Another extension included checks against a list to see if the current user is either a tester or developer. When I develop solutions I will place information on the UI but will hide the information from testers and users. A clear and simple example, the design of the solution dictates that the user is prompted Yes/No to leave the application yet I do not want to be asked so the following is used which is an extension of Windows.Forms which uses an extension to My Namespace.

    Code:
    <Runtime.CompilerServices.Extension()> _
    Sub CloseThisForm(ByRef sender As Windows.Forms.Form)
       If My.CurrentUser.IsTester Then
          If My.Dialogs.Question("Close?") Then
             sender.Close()
          End If
       Else
          sender.Close()
       End If
    End Sub
    In the close button of the main form (handling the form’s control box not handled here)
    Code:
    Me.CloseThisForm()
    Another example, you need to display the system up time.
    Code:
    My.Dialogs.InformationDialog(My.Computer _
                                 .SystemUpTime.ToString, "System up time")
    Console.WriteLine(My.Computer.SystemUpTime.ToString)
    Ouputs 08:52:41.7990000
    Or broken down by hour and minutes
    Code:
    Dim UpTime = My.Computer.SystemUpTime
    Console.WriteLine("{0}:{1}", UpTime.Hours, UpTime.Minutes)
    Outputs 8:54
    For me an extension of My Namespace deals with my database, IBM-DB2 where I target a solution for a specific version of IBM Client Access to be pro-active and stop database access before an exception is raised. The following is not included in the attached project.

    Code:
    If My.iSeries.CorrectVersionInstalled Then
        Console.WriteLine("Perform data operations against DB2")
    Else
        Console.WriteLine(My.iSeries.ExceptionMessage)
    End If
    To see if Client Access is installed
    Code:
    My.iSeries.AssemblyExists

    What version is installed
    Code:
    My.iSeries.InstalledVersion
    Any ways the attached project should help you with My.Dialogs for simplifying showing messages to users and also assist with creating your own partial classes and new classes under My.Namespace.
    • Attached project is VS2008, code should work fine in VS2005 although I have not tested under VS2005 so it might need some tweaking.
    • There are more dialogs than discussed here in the attached zip file
    Attached Files Attached Files
    Last edited by kareninstructor; Oct 21st, 2010 at 07:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width