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

VB.Net - Declaration


May 13, 2021 vb.net


Table of contents


The statement declaration is the complete instruction in the Visual Basic program. It can contain keywords, operators, variables, literal values, constants, and expressions.

Statements can be classified as:

1, Declaration statements - These statements are statements that you name variables, constants, or procedures, or you can specify data types.


2, Executable statements executables - these are the statements that start the action. T hese statements can call methods or functions, loop or branch through blocks of code, or assign values or expressions to variables or constants. In the last case, it is called an Assignment statement.

Declaration statement

Declaration statements are used to name and define procedures, variables, properties, arrays, and constants. /b118> When you declare a programming element of , you can also define its data type, access level, and scope.

The programming elements that you can declare include variables, constants, enumerals, classes, structures, modules, Interfaces, programs, procedure parameters, function return values, references to external procedures, operators, Properties, events, and delegates.

The following is the VB.Net statement in the list
S.N Statements and descriptions Example
1

Dim Statement

Dim statement

Declares and allocates storage space for one or more variables.

Declare and allocate storage space for one or more variables.

Dim number As Integer
Dim quantity As Integer = 100
Dim message As String = "Hello!"
2 Const Statement
Const statement

Declares and defines one or more constants.
Declare and define one or more constants.
Const maximum As Long = 1000
Const naturalLogBase As Object 
= CDec(2.7182818284)
3 Enum Statement
Enumere the statement

Declares an enumeration and defines the values of its members.
Declares an enumeration and defines the value of its members.
Enum CoffeeMugSize
    Jumbo
    ExtraLarge
    Large
    Medium
    Small
End Enum 
4

Class Statement

Class statement

Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises.

Declares the name of the class and introduces definitions of the variables, properties, events, and procedures that the class contains.

Class Box
Public length As Double
Public breadth As Double   
Public height As Double
End Class
5

Structure Statement

Structure declaration

Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises.

Declares the name of the structure and introduces definitions of variables, properties, events, and procedures that the structure contains.

Structure Box
Public length As Double           
Public breadth As Double   
Public height As Double
End Structure
6

Module Statement

The module statement

Declares the name of a module and introduces the definition of the variables, properties, events, and procedures that the module comprises.

Declares the name of the module and describes the definition of the variables, properties, events, and procedures that the module contains.

Public Module myModule
Sub Main()
Dim user As String = 
InputBox("What is your name?") 
MsgBox("User name is" & user)
End Sub 
End Module
7

Interface Statement

Interface statement
Declares the name of an interface and introduces the definitions of the members that the interface comprises.
Declares the name of the interface and describes the definition of the member that the interface contains.

Public Interface MyInterface
    Sub doSomething()
End Interface 
8

Function Statement

The function statement

Declares the name, parameters, and code that define a Function procedure.

Declaration defines the name, parameters, and code of the function procedure.

Function myFunction
(ByVal n As Integer) As Double 
    Return 5.87 * n
End Function
9

Sub Statement

Sub-statement

Declares the name, parameters, and code that define a Sub procedure.

The declaration defines the name, parameters, and code of the Sub procedure.

Sub mySub(ByVal s As String)
    Return
End Sub 
10

Declare Statement

Declaration statement

Declares a reference to a procedure implemented in an external file.

Declares a reference to the procedure implemented in the external file.

Declare Function getUserName
Lib "advapi32.dll" 
Alias "GetUserNameA" 
(
  ByVal lpBuffer As String, 
  ByRef nSize As Integer) As Integer 
11

Operator Statement

The operator declares

Declares the operator symbol, operands, and code that define an operator procedure on a class or structure.

The operator symbol that declares , the operancies, and the code that defines an operator procedure in the class or structure.

Public Shared Operator +
(ByVal x As obj, ByVal y As obj) As obj
        Dim r As New obj
' implemention code for r = x + y
        Return r
    End Operator 
12

Property Statement

Property declaration

Declares the name of a property, and the property procedures used to store and retrieve the value of the property.

Declares the name of the property, as well as the property procedure used to store and retrieve property values.

ReadOnly Property quote() As String 
    Get 
        Return quoteString
    End Get 
End Property
13

Event Statement

Event declaration

Declares a user-defined event.

Declare user-defined events.

Public Event Finished()
14

Delegate Statement

The delegate declares

Used to declare a delegate.

Used to declare a delegate.

Delegate Function MathOperator( 
    ByVal x As Double, 
    ByVal y As Double 
) As Double 

Executable statements

The statement can be executed to perform the action. C alling a procedure, branching to another part of the code, loops a statement that uses several statements or evaluates an expression that is executable. Assignment statements are a special case of executable statements.


Example

The following example demonstrates a decision statement:

Module decisions
   Sub Main()
      'local variable definition '
      Dim a As Integer = 10

      ' check the boolean condition using if statement '
      If (a < 20) Then
         ' if condition is true then print the following '
         Console.WriteLine("a is less than 20")
      End If
      Console.WriteLine("value of a is : {0}", a)
      Console.ReadLine()
   End Sub
End Module


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

a is less than 20;
value of a is : 10