mike mcintyre's

.N e t J o u r n a l

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Source Code

Here is one approach to extending the My namespace. Using this approach an object is served up as a property in Intellisense when My. is typed.

INSTRUCTIONS

Create a Visual Studio 2005 project.

Add the code below to a new project file named:    My Extensions Example.vb

Add a Button1 to Form1.

Add a Button1 click handler to the code behind Form1.

Type  My. into the click handler code area to experiment with the new My.MyExtensions property.

' To the My namespace add a Module named MyExtensionExample.

' In the MyExtensionExample module use a Property to expose

' a MyExtensions object.

Namespace My

    ' Apply the HideModuleName attribute to the MyExtensionExample module.

    ' This will prevent 'MyExtensionExample' from appearing in intellisense.

    ' Instead client code will see only the

    _

    Module MyExtensionExample

        ' Declare a field named _MyExtensions of type MyExtensions.

        ' Instantiate a new MyExtensions object and assign it

        ' to the _MyExtensions variable.

        Private _MyExtensions As New MyExtensions

        ' Declare a property names MyExtensions of type MyExtensions.

        Friend Property MyExtensions() As MyExtensions

            Get

                Return _MyExtensions

            End Get

            Set(ByVal value As MyExtensions)

                _MyExtensions = value

            End Set

        End Property

    End Module

End Namespace

 

' MyExensions Class

' Exposed through MyExtensions property

' in the MyExtensionExample module.

Public Class MyExtensions

 

    ' Example member for MyExtensions class.

    Public ReadOnly Property GetPathToTextFile() As String

        Get

            ' Declare a variable named textOpenFiledDialog of type OpenFileDialog.

            ' Instantiate a new OpenFileDialog object and assign

            ' it to the textOpenFileDialog variable.

            Dim textOpenFileDialog As New OpenFileDialog

            ' Set the CheckFileExists property of the OpenFileDialog to True

            ' to ensure a valid file path is returned.

            textOpenFileDialog.CheckFileExists = True

            ' Set the Title property of the OpenFileDialog.

            textOpenFileDialog.Title = "Open a Text File"

            ' Set the Filter property of the OpenFileDialog so that

            ' .txt files will be shown in the dialog.

            textOpenFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"

            ' Show the OpenFileDialog.

            ' If the user clicks the OK button on the OpenFileDialog...

            If textOpenFileDialog.ShowDialog = DialogResult.OK Then

                ' Return the FileName property of the OpenFileDialog; this

                ' will be a path to a file as a string.

                Return textOpenFileDialog.FileName

            Else

                ' User did not pick a file so return Nothing.

                Return Nothing

            End If

        End Get

    End Property

 

End Class

posted on Sunday, August 29, 2004 1:01 PM