Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VB.Net - Basic syntax


May 13, 2021 vb.net


Table of contents


Vb. N et is an object-oriented programming language. I n object-oriented programming methods, programs consist of various objects that interact with each other through actions. T he actions an object might take are called methods. O bjects of the same type are considered to have the same type, or are more often referred to in the same class.


When we consider VB.Net, it can be defined as a collection of objects that communicate by calling each other's methods. Now let's take a look at what classes, objects, methods, and instance variables mean.


  • Object Object - Objects have states and behaviors. E xample: Dogs have states - color, name, breed, and behavior - swinging, barking, eating, etc. A n object is an instance of a class. ass. xample: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating, etc. An object is an instance of a class.

  • Class Class - A class can be defined as a template/blueprint that describes the behavior/state supported by objects of its type. A class can be defined as a template/blueprint that describes the behaviors/states that objects of its type support.

  • Methods Method - Method is basically a behavior. A class can contain many methods. I t is in a way that writes logic, manipulates data, and performs all the actions. ted. 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 own unique set of instance variables. T he state of the object is created by the values assigned to these instance variables. les. An object's state is created by the values assigned to these instance variables.


Vb. Rectangle class in Net

For example, let's consider a Rectangle object. I t has properties such as length and width. B y design, it may need to accept the values of these properties, calculate the area, and display details in a way.


Let's look at the implementation of a Rectangle class and discuss the basic syntax VB.Net our observations:

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


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

Length: 4.5
Width: 3.5
Area: 15.75

In the last chapter, we created a Visual Basic module that contains code. S ub Main represents VB.Net entry point for the program. H ere, we use a class that contains code and data. Y ou use classes to create objects. F or example, in code, r is a Rectangle object.


An object is an instance of a class:

Dim r As New Rectangle()

A class can have members that can be accessed from an external class, if specified. Data members are called fields, and process members are called methods.

Shared or static methods can be called without creating objects for the class. Call the instance method through an object of the class:

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


Identifier

An identifier is the name used to identify a class, variable, function, or any other user-defined item. T he basic VB.Net to name a class in a class is as follows:

  • The name must begin with a letter, followed by a letter, number (0 - 9), or underscore. T he first character in the identifier cannot be a number. git. The first character in an identifier cannot be a digit.

  • What is it like if it can't contain any embedded spaces or symbols? - +! @ #%^&*()[] {}。 ; : ''and'. H owever, you can use underscores. sed. " ' / and \. However, an underscore ( _ ) can be used.

  • It should not be a reserved keyword. It should not be a reserved keyword.


Vb. Net keywords

The following table lists VB.Net keywords that are retained:

AddHandler AddressOf Alias And AndAlso As Boolean
Byref Byte ByVal Call Case Catch CBool
CByte CChar CDate CDec CDbl Char CInt
Class CLng CObj Const Continue CSByte CShort
CSng CStr CType CUInt CULng CUShort Date
Decimal Declare Default Delegate Dim DirectCast Do
Double Each Else ElseIf End End If Enum
Erase Error Event Exit False Finally For
Friend Function Get GetType

GetXML

Namespace

Global GoTo
Handles If Implements Imports In Inherits Integer
Interface Is IsNot Let Lib Like Long
Loop Me Mod Module MustInherit MustOverride MyBase
MyClass Namespace Narrowing New Next Not Nothing

Not

Inheritable

Not

Overridable

Object Of On Operator Option
Optional Or OrElse Overloads Overridable Overrides ParamArray
Partial Private Property Protected Public RaiseEvent ReadOnly
ReDim REM

Remove

Handler

Resume Return SByte Select
Set Shadows Shared Short Single Static Step
Stop String Structure Sub SyncLock Then Throw
To True Try TryCast TypeOf UInteger While
Widening With WithEvents WriteOnly Xor