Visual Basic Statements
By: Steven Holzner in VB.net Tutorials on 2008-11-25
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")
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures 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
Throwing an Exception in VB.net
Comments