By: Steven Holzner Emailed: 1728 times Printed: 2331 times
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")
Be the first one to add a comment
Most Viewed Articles (in VB.net )
Most Emailed Articles (in VB.net)