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

VB.Net - variable


May 13, 2021 vb.net


Table of contents


A variable is just the name of the storage area that gives our program actionable. V b. E ach variable in Net has a specific type that determines the size and layout of the variable memory; t he range of values that can be stored in the memory; and a set of actions that can be applied to the variable.

We've discussed various data types. V b. The basic value types provided in Net can be divided into:

Type Example
Integral types SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong and Char
Floating point types Single and Double
Decimal types Decimal
Boolean types True or False values, as assigned
Date types Date
Vb. N et also allows you to define variables of other value types, such as enumeration and reference types for class variables. W e'll discuss date types and classes in a later section.


Vb. Variable declarations in Net

Dim statements are used for variable declarations and storage allocations for one or more variables. Dim statements are used at the module, class, structure, procedure, or block level.

Vb. The syntax of variable declarations in Net is:

[ < attributelist> ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]]
[ ReadOnly ] Dim [ WithEvents ] variablelist

1, attributelist is a list of properties that apply to variables. Optional.

2, accessmodifier defines the access level of the variable, which has values - Public, Protected, Friend, Protected Friend, and Private. O ptional.


3. Shared shared declares a shared variable that is not associated with any particular instance of a class or structure, but can be used for all instances of a class or structure. Optional.

4, Shadows shadow indicates that a variable redeslaids and hides an element of the same name or a set of overloaded elements in the base class. Optional.

5, Static means that the variable will retain its value, even after the process of declaring it is terminated. Optional.

6, ReadOnly indicates that variables can be read, but not written. Optional.

7, WithEvents specifies that the variable is used to respond to events raised by instances assigned to variables. Optional.

8, Variablelist provides a list of declared variables.


Each variable in the list of variables has the following syntax and sections:

variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]

1, variablename: is the name of the variable

2, boundslist: optional. It provides a list of boundaries for each dimension of the array variable.

3, New: Optional. When the Sim statement runs, it creates a new instance of the class.

4, datatype: If Option Strict is On, it is required. It specifies the data type of the variable.

5, initializer: If New is not specified, it is optional. An expression that is evaluated and assigned to a variable when it is created.


Some valid variable declarations and their definitions are as follows:

Dim StudentID As Integer
Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim exitButton As New System.Windows.Forms.Button
Dim lastTime, nextTime As Date


Vb. The variable initialization in Net

The variable is initialized (assigned) an equal sign, followed by a constant expression. T he general form of initialization is:

variable_name = value;


For example

Dim pi As Double
pi = 3.14159


You can initialize the variable at the time of declaration, as follows:

Dim StudentID As Integer = 100
Dim StudentName As String = "Bill Smith"


Example

Try the following example, which uses various types of variables:

Module variablesNdataypes
   Sub Main()
      Dim a As Short
      Dim b As Integer
      Dim c As Double
      a = 10
      b = 20
      c = a + b
      Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c)
      Console.ReadLine()
   End Sub
End Module


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

a = 10, b = 20, c = 30


Accepts the value from the user

The console class in the System namespace provides a function, ReadLine, to accept input from the user and store it in variables. F or example

Dim message As String
message = Console.ReadLine


Here's an example:

Module variablesNdataypes
   Sub Main()
      Dim message As String
      Console.Write("Enter message: ")
      message = Console.ReadLine
      Console.WriteLine()
      Console.WriteLine("Your Message: {0}", message)
      Console.ReadLine()
   End Sub
End Module


When the above code is compiled and executed, it produces the following results (assuming that the user enters Hello World):

Enter message: Hello World   
Your Message: Hello World


Lvalues and Rvalues

There are two expressions:

  • lvalue: Expressions that are left values may appear on the left or right side of the assignment.
  • rvalue: Expressions that are right values may appear on the right side of the job but not on the left.


The variable is a left value, so it may appear on the left side of the job. T he numeric text is the right value, so it may not be assigned and cannot appear on the left. H ere are the valid statements:

Dim g As Integer = 20


However, the following statements are not valid and generate compile-time errors:

20 = g