Thursday, November 1, 2012

Use the Module in VB NET

Use the Module in VB NET

Is sometimes useful to have functions or variables available everywhere in a Visual Basic project. You could use a module to do so. It is not complicate. There is no class creation.  And you could easily use this technique in VBA or VB6 (Visual Basic Application, Visual Basic Enterprise VB6, …) Module could also be used to share information or objects between classes.
Start creating a project. I choose the Windows Forms Project. Here is a sample of form1.


''' <summary>
''' put a Timer without the Visual Basic form Designer
''' </summary>
''' <remarks></remarks>
Public Class Form1
    Private WithEvents oTimer As System.Windows.Forms.Timer

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        oTimer = New System.Windows.Forms.Timer
        oTimer.Interval = 1000
        oTimer.Enabled = True
        oTimer.Start()
        If oTimer Is Nothing Then
            MsgBox("oTimer is Nothing")
        End If

        Dim oForm2 As New Form2
        oForm2.Show()
    End Sub

    Private Sub timerTicks() Handles oTimer.Tick
        Me.Text = Date.Now
        Module1.sTime = Date.Now
    End Sub

End Class






Create a form2 :


Public Class Form2

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub updateme() Handles Me.MouseDown
        Me.Text = Module1.sTime
        Me.Refresh()
    End Sub
End Class


Now create Module.vb.

Module Module1
    Public sTime As String
End Module



Here is how my solution explorer looks like this.


Alright, my code is really stupid, I know! But I only wanted to show you something very interesting.
On form1, there is a timer running over and over. Every second, it displays the time.  In fact, is nothing more than a simple clock. But the clock saves the time in Module1.sTime
Now, imagine I have another called form2. That second form would like get the timer value but it can’t directly. There is no methods to give the information, the variable is private, no heritage, NOTHING!!!!

Is time to use the Module. The advantage is that Modules are “available everywhere” at any time the module variables, properties, methods are also accessible as long there are public. That is all. Just keep that in mind.


So run your code, when your hold down your mouse button over the form2, it reads the value inside Module1. You could pass any object from one class to another simply by using a module.

By the way, feel  free to change your code and make it suitable for you:



    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim oForm2 As Form2
        Try 'alway try to put code that might crash in a try-catch
            oTimer = New System.Windows.Forms.Timer
            With oTimer
                .Interval = 1000
                .Enabled = True
                .Start()
            End With
           If oTimer Is Nothing Then MsgBox("oTimer is Nothing")
            oForm2 = New Form2
            oForm2.Show()
        Catch ex As Exception
            MsgBox("ERRRRRROR !!!! " & vbCrLf & ex.Message) 'idiot! you made a error
        End Try
    End Sub





Buy the same edition I bought:

or visite my web site :




No comments:

Post a Comment