VB.net Basic Syntax

VB.net Syntax

Visual Basic has a very simple programming VB.net Basic Syntax.

The language is not case-sensitive, and it is easy even for beginners to start coding.

The VB.net programming language is an object-oriented programming language.

A program in the Object-Oriented Programming approach is made up of many objects that interact with one another through actions.

Methods are the actions that an object can perform. Objects of the same type are said to be of the same type, or more commonly, of the same class.

A VB.net application can be characterized as a collection of objects that communicate by invoking the methods of each other.

Let’s take a quick look at the definitions of class, object, methods, and instance variables.

1. 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.

2. Class

A class can be defined as a template/blueprint that describes the behaviors/states that objects of its type support.

3. Methods

A method is basically a behavior. A class can contain many methods. It is in methods where the logic is written, data is manipulated and all the actions are executed.

4. 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

Consider the Rectangle object as an example. It has dimensions such as length and width.

Depending on the design, methods for taking these attributes’ values, calculating area, and presenting details may be required.

Let’s take a look at a Rectangle class implementation and analyze VB.net fundamental syntax based on our findings.

Imports System
Public Class Rectangle
   Private length As Double
   Private width As Double

   'Public methods
   Public Sub AcceptDetails()
      length = 4.5
      width = 3.5
   End Sub

   Public Function GetArea() As Double
      GetArea = length * width
   End Function
   Public Sub Display()
      Console.WriteLine("Length: {0}", length)
      Console.WriteLine("Width: {0}", width)
      Console.WriteLine("Area: {0}", GetArea())

   End Sub

   Shared Sub Main()
      Dim r As New Rectangle()
      r.Acceptdetails()
      r.Display()
      Console.ReadLine()
   End Sub
End Class

You can test the above example here! ➡ VB.net Online Compiler

When the above code is compiled and executed, it produces the following result:

Length: 4.5
Width: 3.5
Area: 15.75

Here, we are using a 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 the 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.

Shared Sub Main()
   Dim r As New Rectangle()
   r.Acceptdetails()
   r.Display()
   Console.ReadLine()
End Sub

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:

AddHandlerAddressOfAliasAndAndAlsoAsBoolean
ByRefByteByValCallCaseCatchCBool
CByteCCharCDateCDecCDblCharCInt
ClassCLngCObjConstContinueCSByteCShort
CSngCStrCTypeCUIntCULngCUShortDate
DecimalDeclareDefaultDelegateDimDirectCastDo
DoubleEachElseElseIfEndEnd IfEnum
EraseErrorEventExitFalseFinallyFor
FriendFunctionGetGetTypeGetXML NamespaceGlobalGoTo
HandlesIfImplementsImportsInInheritsInteger
InterfaceIsIsNotLetLibLikeLong
LoopMeModModuleMustInheritMustOverrideMyBase
MyClassNamespaceNarrowingNewNextNotNothing
Not InheritableNot OverridableObjectOfOnOperatorOption
OptionalOrOrElseOverloadsOverridableOverridesParamArray
PartialPrivatePropertyProtectedPublicRaiseEventReadOnly
ReDimREMRemove HandlerResumeReturnSByteSelect
SetShadowsSharedShortSingleStaticStep
StopStringStructureSubSyncLockThenThrow
ToTrueTryTryCastTypeOfUIntegerWhile
WideningWithWithEventsWriteOnlyXor
List of VB.net Keywords

Summary

Programming in Visual Basic is really straightforward. Even if you are not a programmer, the majority of the code is simple to grasp.

Inline comments begin with an apostrophe, while XML comments begin with three apostrophes.

The language is not case-sensitive.


Leave a Comment