VB.Net - Basic Syntax
VB.Net is an object-oriented programming language. In Object-Oriented Programming methodology, a program consists of various objects that interact with each other by means of actions. The actions that an object may take are called methods. Objects of the same kind are said to have the same type or, more often, are said to be in the same class.
When we consider a VB.Net program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods and instance variables mean.
Ø Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating, etc. An object is an instance of a class.
Ø Class − A class can be defined as a template/blueprint that describes the behaviors/states that objects of its type support.
Ø Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
Ø Instance Variables − Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.
A Rectangle Class in VB.Net
For example, let us consider a Rectangle object. It has attributes like length and width. Depending upon the design, it may need ways for accepting the values of these attributes, calculating area and displaying details.
Let us look at an implementation of a Rectangle class and discuss VB.Net basic syntax on the basis of our observations in it −
When the above code is compiled and executed, it produces the following result −
Length: 4.5
Width: 3.5
Area: 15.75
In previous chapter, we created a Visual Basic module that held the code. Sub Main indicates the entry point of VB.Net program. Here, we are using Class that contains both code and data. You use classes to create objects. For example, in the code, r is a Rectangle object.
An object is an instance of a class −
Dim r As New Rectangle()
A class may have members that can be accessible from outside class, if so specified. Data members are called fields and procedure members are called methods.
Shared methods or static methods can be invoked without creating an object of the class. Instance methods are invoked through an object of the class −
Identifiers
An identifier is a name used to identify a class, variable, function, or any other user-defined item. The basic rules for naming classes in VB.Net are as follows −
· A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9) or underscore. The first character in an identifier cannot be a digit.
· It must not contain any embedded space or symbol like ? - +! @ # % ^ & * ( ) [ ] { } . ; : " ' / and \. However, an underscore ( _ ) can be used.
· It should not be a reserved keyword.
VB.Net Keywords
The following table lists the VB.Net reserved keywords −