Variables in VB.NET, vb.net, variables

In this article you will learn what is variables in VB.NET and how to declare and initialization variables.
  • 2524

In any programming language, variable store value during a program's execution.

I am writing a program that converts amounts between different currencies.

Example: In this example calculate and display the discounted price for the amount of 50000.

Module Module1

    Sub Main()
        Dim amount As Single
        Dim discount As Single
        Dim discamount As Single
        amount = 50000
        discount = 0.5
        discamount = amount * (1 - discount)
        MsgBox("your price is $" & discamount)
    End Sub

End Module

Note: Single is a numeric data type, it can store both integer and non- integer values.

Output :

image1.gif
 

Declaring Variables:

You can declare integer variables:

Dim width As Integer

You can declare multiple variables of the same type having to repeat each variable's type:

In this statement will create three integer variables:

Dim width, depth, height, as Integer

In this statement will create two integer and two Double variables:

Dim width, height As Integer, area, volume As Double

T
o declare a variable, use the Dim statement followed by the variable's name, the AS keyword, and its type, like:

Dim height As Integer

Dim greetings As String

When declare variables, here are steps to use:

  • Start with a letter.

  • you can use only special character that can appear in the variable's name is the underscore character.

  • Limits of the characters is 255.

  • Must be unique within the scope.

  • Variable names in VB.NET is case-insensitive.

Variable initialization

we can initialize variables in the same line that declare them.

Here declares an integer variables and initializes in to 2,450:

Dim instance as Interger = 2450

Conclusion

In this article you learned what is variables in VB.NET and how to declare and initialization variables. In next article you will learn type of variables.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.