How to use Variables, Method, Properties, Events in VB .NET
In this article you will learn how to use Variables, Method, Properties, Events in Visual Basic .NET.
In this article you will learn how to use Variables, Method, Properties, Events in Visual Basic .NET.
Classes and Objects: Classes are types and Objects are instance of the class. They are very much related to each other. The Class can not be called without an Object. In Visual Basic we create a class by using Class keyword and end it with End Class. The syntax of creating a class is as follow:
Public Class First
----Events
----Variables
----Methods
----Properties
End Class
Events, Variables, Methods, Properties:
Events: Events are the message sent by an object to the main program loop, these messages have some information about some specific occurrence that takes place. For example when we click a button a click event occurs and we can handle that event with the help of Event handler.
Variables: A variable is the name given to a memory location holding particular type of data. So, each variable has associated with it a data type and value. Variable can be read or set directly. In Visual Basic .Net, a variables is declared as:
Public Class Library
Public Books As Integer
'declaring a variable named Books of Integer type
End Class
Properties: They are retrieved and set like variables. Property Get and Property Set procedures which provide more control on how values are set use to implement the properties.
Methods: Methods represent the object's built-in procedures. For example, a class named First have a method named Display. We can define a method as given blow:
Public Class First
'creating a class named First
Sub Display()
'creating a method named Display in the class
End Sub
End Class
We use New keyword to create the Object for this class and is like Dim obj As New First(). Now, I show you how to create a class, method and access the class with an object.
Example:
Imports System.Console
Module Module1
Sub Main()
Dim obj As New First()
'Creating a object obj for First class
obj.Display()
'calling Display method using obj
Read()
End Sub
End Module
Public Class First
'creating a class named First
Sub Display()
'creating a method named Display in the class
Write("Hello World")
End Sub
End Class
The output of this program is in img1.1
Summary:
Hope this article help you to learn about Classes, Objects, Variables, Methods, Properties and Events.