ARTICLE

Variables in VB.NET

Posted by Dinesh Beniwal Articles | Visual Basic Language July 14, 2010
In this article you will learn what is variables in VB.NET and how to declare and initialization variables.
 
Reader Level:

HTML clipboard

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor