Visual Basic Statements

By: Steven Holzner Emailed: 1603 times Printed: 2075 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

A Visual Basic statement is a complete instruction. It can contain:

  • keywords—Words reserved for Visual Basic's use.

  • operators—Symbols used to perform operations, like +, which performs addition operations; -, which performs subtraction operations; and so on.

  • variables—Symbolic names given to values stored in memory and declared with the Dim keyword. For example, if you've declared a variable named temperature as an Integer type, you can store integer values like 72 or 83 in it.

  • literal values—Simple values, like 5 or "Hello."

  • constants—The same as variables, except that constants are assigned a value that cannot then be altered.

  • expressions—Combinations of terms and/or keywords that yield a value. For example, if the variable temperature holds the value 72, then the expression temperature + 3 yields the value 75.

Each statement is one of the following:

  • A declaration statement, which can name and create a variable, constant, or procedure and can also specify a data type. The data type can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, or String; or the name of an enumeration (a series of constants defined at the same time), structure, class, or interface.

  • An executable statement, which can perform an action. These statements can execute a method or function, or they can loop through code using one of the Visual Basic loops , which execute a series of statements repetitively, or they can be an assignment statement, which assign a value or expression to a variable or constant such as this: temperature = 72, and so on.

Besides having single statements in your code, statements can also be grouped into blocks, as we'll see when we discuss conditions like the If statement, which might look like this, where I'm testing the value in the variable BankBalance to see if it's less than 0:

Module Module1

    Sub Main()
        Dim BankBalance As Single = 500.01
        If (BankBalance < 0) Then
            Dim strError As String
            strError = "Hey, your bank balance is negative!"
            System.Console.WriteLine(strError)
        End If
    End Sub

End Module

In this case, I've surrounded three statements inside an If statement, which starts with If (BankBalance < 0) Then and ends with End If, creating a block of statements. The statement that uses the Dim keyword is a declaration statement that creates the variable BankBalance and gives it the value 500.01. When you create a variable like this you can also set its data type, selecting Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, or String, or the name of an enumeration, structure, class, or interface.

Statements like System.Console.WriteLine("Press Enter to continue") are execution statements that perform actions. Note also the statement strError = "Hey, your bank balance is negative!"—this is an assignment statement (a type of execution statement) that I'm using to assign the text "Hey, your bank balance is negative!" to the variable strError. To be able to handle text strings this way, I've declared strError as a variable of type String. After I've declared strError, I can assign strings to this variable using the = assignment operator.

You also can have multiple statements on the same line in VB .NET, if you separate them with a colon (:), as I'm doing here, where I'm declaring a variable named temperature (you need to declare variables before using them) and giving it a value of 72:

Dim temperature As Integer : temperature = 72

Conversely, you can also break long statements up over several lines if you want, as long as you use an underscore (_) at the end of each line, like this, where I'm assigning a value to the temperature variable:

Dim temperature As Integer
temperature = 1 + 2 + 3 _
+ 4 + 5 + 6 _
+ 7 + 8 + 9 + 10

You have to be careful about strings of text, which are enclosed in quotation marks in Visual Basic. If you want to break a statement in the middle of a string, you must divide the string into two or more strings, using the & string concatenation operator (+ can also function as a concatenation operator) to tell Visual Basic that you want multiple strings joined together into one, like this:

System.Console.WriteLine("This is a very long sentence that I " _
& "want to display to the user")

VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
How to export from database to excel using VB.net
Your first VB.NET Crystal Reports - A step by step guide
How To Connect To A MySql Database in VB.net
How to export from DataGridView to excel using VB.net
Visual Basic Statements
Insert cell data in an Excel file using OLEDB in VB.net
Creating Tree Views in Code using VB.net
Changes in Controls from VB6 to VB.net
What's New in VB .NET? A comparison of VB vs VB.net
Assemblies, Solutions and Projects in VB .NET
String.Contains (), compare, clone in VB.net
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Using Excel 2007 DataBar in VB.net
Client Socket Program sample in VB.net
Structured Exception Handling in VB.net
Most Emailed Articles (in VB.net)
For Loop in VB.net
For Each…Next Loop in VB.net
How to export from DataGridView to excel using VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Raising an Exception Intentionally in VB.net
“Using If with And” and Comparing two integers using If
What is .NET Framework and the Common Language Runtime?
Data types in VB.net
String equals, Copy to & Copy to in VB.net
Handling Timer Events - and Creating an Alarm Clock in VB.net
Properties in VB.net
The Select Case statement in VB.net
File Extensions Used in VB .NET
Sub Procedures and Functions in VB.net
String.Contains (), compare, clone in VB.net