WHAT'S NEW?
Loading...

Create Module and Class in VB.Net



How to create Module and Class in VB.Net? Both of them have capabilities to handled all controls and more or less the same functionality, however there’s something difference among them.
Obviously, the declaration of variables and how they construct the proper syntax, it depends on the programmer’s side. In this tutorial, I’ll show you the difference of Module and Class in constructing code, but the same output.

These code are used in Module and Class and place it on different button.

Public Class FrmModuleAndClass

    Private Sub btnmodule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmodule.Click

        Dim myhold As String
        '________________________________________________________'
        'I declared variable myhold to hold mymodule,
        'the name of my module                                   '
        '________________________________________________________'
        myhold = mymodule.getdata(txtfnamemodule.Text, txtlnamemodule.Text)
        MessageBox.Show(myhold, "My Module")
        '----------------------------------------

        'above code call mymodule that returns parameters
    End Sub

    Private Sub btnclass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclass.Click

        Dim myhold As New myclassname
        '________________________________________________________'
        'I declared myhold variable to hold my new object name   '
        'myclassname, the name of my class                       '
        '________________________________________________________'

        Dim holdmydata As String
        '________________________________________________________'
        'above code holdmydata hold myclassname class            '
        '________________________________________________________'

        holdmydata = myhold.getdata(txtfnameclass.Text, txtlnameclass.Text)
        MessageBox.Show(holdmydata, "My Class")
        '----------------------------------------

        'above code call myclass that returns parameters
    End Sub
End Class
______________________________________________________
Code for my Module

Module mymodule

    Public Function getdata(ByVal Fname As String, ByVal Lname As String) 'this line of code return 2 parameter value

        Dim myname As String

        myname = Fname & " " & Lname
        Return myname

    End Function
End Module

Code for my Class

Public Class myclassname

    Public myname As String

    Public Function getdata(ByVal Fname As String, ByVal Lname As String) 'this line of code return 2 parameter value

        myname = Fname & " " & Lname

        Return myname

    End Function
End Class

My Sample Screen shot:


Have you noticed when you declared variable name? In Class, first you create Dim VarName as New ClassObjectName to tell visual basic that you are setting up a variable name, while Module there's no need to create, that's the difference. You could not write directory the code without declaring New ClassObjectName from the Class name. 

0 comments:

Post a Comment