Programming Languages
A Dave Hillman Project

[Home] [Contact Me] [Site Map] [Search] [References]

VB.Net

 

[Up]

[Language Basics]
[C Language]
[C++]
[C#]
[Java]
[JavaScript]
[Visual Basic 6.0]
[VBScript]
[VB.Net]
[.Net Technology]

 

 

Introduction

Visual Basic.Net (VB.Net) is most recent implementation of the long-running Visual Basic language series. 

For all intents and purposes, VB.Net is a new implementation taking advantage of the .Net architecture.  It is possible to create a range of VB.Net applications ranging from command line applications, desktop applications, and web applications.

Visual Basic .NET has many new language features including:

bulletInheritance, interfaces, and overloading that make it a true object-oriented programming language.
bulletThe ability create multithreaded, scalable applications using explicit multithreading.
bulletStructured exception handling, custom attributes.
bulletCommon language specification (CLS) compliance.
bulletThe CLS is a set of rules that standardizes such things as data types and how objects are exposed and interoperate.
bulletAny CLS-compliant language can use the classes, objects, and components you create in Visual Basic .NET.
bulletA VB.Net user, can access classes, components, and objects from other CLS-compliant programming languages without worrying about language-specific differences such as data types.
bulletCLS features used by Visual Basic .NET programs include assemblies, namespaces, and attributes.

Language Fundamentals

Program Structure and Organization

bulletCommenting code
bullet' comment (anywhere in line)
x = 1   ' comment
Rem comment
bulletLanguage/character sets
bulletIdentifiers and keywords:

 

Alias Ansi As Assembly
Auto ByRef ByVal Case
Default DirectCast Each Else
ElseIf End Error Explicit
False For Friend Handles
In Is Lib Loop
Me Module MustInherit MustOverride
MyBase MyClass New Next
Nothing NotInheritable NotOverridable Off
On Option Optional Overloads
Overridable Overrides
ParamArray Preserve Private Protected
Public ReadOnly Resume Shadows
Shared Static Step Then
To True TypeOf Unicode
Until When While WithEvents
WriteOnly

 

Data Types

 

Visual Basic type Common language runtime type structure Nominal storage allocation Value range
Boolean System.Boolean 2 bytes True or False.
Byte System.Byte 1 byte 0 through 255 (unsigned).
Char System.Char 2 bytes 0 through 65535 (unsigned).
Date System.DateTime 8 bytes 0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999.
Decimal System.Decimal 16 bytes 0 through +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;
0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest nonzero number is
+/-0.0000000000000000000000000001 (+/-1E-28).
Double
(double-precision floating-point)
System.Double 8 bytes -1.79769313486231570E+308 through
-4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.
Integer System.Int32 4 bytes -2,147,483,648 through 2,147,483,647.
Long
(long integer)
System.Int64 8 bytes -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807.
Object System.Object (class) 4 bytes Any type can be stored in a variable of type Object.
Short System.Int16 2 bytes -32,768 through 32,767.
Single
(single-precision floating-point)
System.Single 4 bytes -3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values.
String
(variable-length)
System.String (class) Depends on implementing platform 0 to approximately 2 billion Unicode characters.
User-Defined Type
(structure)
(inherits from System.ValueType) Depends on implementing platform Each member of the structure has a range determined by its data type and independent of the ranges of the other members.

 

bulletVoid/null
bulletConstants
bulletVariable scope

Expressions and Operators

 

Additive  
  Addition +
  Subtraction -
Multiplicative  
  Multiplication *
  Division /
  Integer division \
  Modulus (division returning only the remainder) Mod
  Exponentiation ^
Assignment  
  Assignment =
  Addition +=
  Subtraction -=
  Multiplication *=
  Division /=
  Integer division \=
  Concatenate &=
  Left shift <<=
  Right shift >>=
Relational and equality  
  Less than <
  Less than or equal to <=
  Greater than >
  Greater than or equal to >=
  Equal =
  Not equal <>
  Compare two object reference variables Is
  Compare object reference type TypeOf x Is Class1
  Compare strings =
  Concatenate strings &
  Shortcircuited Boolean AND AndAlso
  Shortcircuited Boolean OR OrElse
Shift  
  Left shift <<
  Right shift >>
Scope resolution  
  Scope resolution .
Postfix2  
  Array element ()
  Function call ()
  Type cast Cint, CDbl, ..., CType
  Member selection .
  Postfix increment n/a
  Postfix decrement n/a
Unary3  
  Indirection n/a
  Address of AddressOf
  Logical-NOT Not
  One's complement Not
  Prefix increment n/a
  Prefix decrement n/a
  Size of type n/a
  comma n/a
Bitwise  
  Bitwise-AND And
  Bitwise-exclusive-OR Xor
  Bitwise-inclusive-OR Or
Logical  
  Logical-AND And
  Logical-OR Or
Conditional  
  Conditional IIf Function ()

 

 

 

Control Statements

bulletIf-then
If condition [ Then ]
   [ statements  ]
[ ElseIf elseifcondition [ Then ]
   [ elseifstatements ] ]
[ Else
   [ elsestatements ] ]
End If

-or-

If condition Then [ statements ] [ Else elsestatements ]

Parts

condition
Required. Expression. The expression you supply for condition must evaluate to True or False, or to a data type that is implicitly convertible to Boolean.
statements
Optional in multiple-line form; required in single-line form that has no Else clause. One or more statements following If...Then that are executed if condition is True.
elseifcondition
Required if ElseIf is present. Same as condition.
elseifstatements
Optional. One or more statements following ElseIf...Then that are executed if the associated elseifcondition is True.
elsestatements
Optional in multiple-line form; required in single-line form that has an Else clause. One or more statements that are executed if no previous condition or elseifcondition expression is True.
End If
Terminates If...Then block.
bulletSelect/switch

Executes one of several groups of statements, depending on the value of an expression.

Select [ Case ] testexpression
   [ Case expressionlist
      [ statements ] ]
   [ Case Else
      [ elsestatements ] ]
End Select

Parts

testexpression
Required. Expression. Must evaluate to one of the elementary data types (Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, Short, Single, and String).
expressionlist
Required in a Case statement. List of expression clauses representing match values for testexpression. Multiple expression clauses are separated by commas. Each clause can take one of the following forms:
bulletexpression1 To expression2
bullet[ Is ] comparisonoperator expression
bulletexpression

Use the To keyword to specify the boundaries of a range of match values for testexpression. The value of expression1 must be less than or equal to the value of expression2.

Use the Is keyword with a comparison operator (=, <>, <, <=, >, or >=) to specify a restriction on the match values for testexpression. If the Is keyword is not supplied, it is automatically inserted before comparisonoperator.

The form specifying only expression is treated as a special case of the Is form where comparisonoperator is the equal sign (=). This form is evaluated as testexpression = expression.

The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparisonoperator is valid for the two types it is being used with.

statements
Optional. One or more statements following Case that are executed if testexpression matches any clause in expressionlist.
elsestatements
Optional. One or more statements following Case Else that are executed if testexpression does not match any clause in the expressionlist of any of the Case statements.
End Select
Terminates Select...Case block.
bulletDo/While loop
Repeats a block of statements while a Boolean condition is True or until the condition becomes True.
Do { While | Until } condition
   [ statements ]
[ Exit Do ]
   [ statements ]
Loop

-or-

Do
   [ statements ]
[ Exit Do ]
   [ statements ]
Loop { While | Until } condition

Parts

While
Required unless Until is used. Keyword. Repeat the loop until condition is False.
Until
Required unless While is used. Keyword. Repeat the loop until condition is True.
condition
Optional. Boolean. Expression that evaluates to a value of True or False.
statements
Optional. One or more statements that are repeated while, or until, condition is True.
bulletWhile/End

Executes a series of statements as long as a given condition is True.

While condition
   [ statements ]
End While

Parts

condition
Required. Expression. Must evaluate to True or False. If condition is Nothing, condition is treated as False.
statements
Optional. One or more statements following While that are executed while condition is True.
End While
Terminates execution of the While block.

 

bulletFor loop

Repeats a group of statements a specified number of times.

For counter [ As datatype ] = start To end [ Step step ]
   [ statements ]
[ Exit For ]
   [ statements ]
Next [ counter ]

Parts

counter
Required. Variable. The data type of counter is usually Integer but can be any elementary numeric type that supports the greater than or equal to (>=), less than or equal to (<=), and addition (+) operators.
datatype
Required if counter is not already declared. Data type of counter. If counter is declared outside this loop, you cannot use the As clause to redeclare it.
start
Required. Expression. The initial value of counter. The start expression usually evaluates to type Integer but can evaluate to any data type that widens to the type of counter.
end
Required. Expression. The final value of counter. The end expression usually evaluates to type Integer but can evaluate to any data type that widens to the type of counter.
step
Optional. Expression. The amount by which counter is incremented each time through the loop. The step expression usually evaluates to type Integer but can evaluate to any data type that widens to the type of counter. If not specified, step defaults to 1.
statements
Optional. One or more statements between For and Next that are executed the specified number of times.

 

bulletFor Each Loop

Repeats a group of statements for each element in a collection.

For Each element [ As datatype ] In group
   [ statements ]
[ Exit For ]
   [ statements ]
Next [ element ]

Parts

element
Required. Variable. Used to iterate through the elements of the collection. The data type of element must be such that the data type of the elements of group can be converted to it.
datatype
Required if element is not already declared. Data type of element. If element is declared outside this loop, you cannot use the As clause to redeclare it.
group
Required. Object variable. Must refer to an object collection or array.
statements
Optional. One or more statements between For Each and Next that are executed on each item in group.
bulletTry Catch Finally

Provides a way to handle some or all possible errors that may occur in a given block of code, while still running code.

Try
   [ tryStatements ]
[ Catch [ exception [ As type ] ] [ When expression ] 
   [ catchStatements ] ]
[ Exit Try ]
...
[ Finally
   [ finallyStatements ] ]
End Try

Parts

tryStatements
Optional. Statement(s) where an error can occur. Can be a compound statement.
Catch
Optional. Multiple Catch blocks permitted. If an exception occurs while processing the Try block, each Catch statement is examined in textual order to determine if it handles the exception. Exception represents the exception that has been thrown.
exception
Optional. Any variable name. The initial value of exception is the value of the thrown error. Used with Catch to specify the error caught.
type
Optional. Specifies the type of class filter. If the value of exception is of the type specified by type or of a derived type, the identifier becomes bound to the exception object.
When
Optional. A Catch statement with a When clause will only catch exceptions when expression evaluates to True. A When clause is only applied after checking the type of the exception, and expression may refer to the identifier representing the exception.
expression
Optional. Must be implicitly convertible to Boolean. Any expression that describes a generic filter. Typically used to filter by error number. Used with When keyword to specify circumstances under which the error is caught.
catchStatements
Optional. Statement(s) to handle errors occurring in the associated Try block. Can be a compound statement.
Exit Try
Optional. Keyword that breaks out of the Try...Catch...Finally structure. Execution resumes with the Finally block if present, otherwise with the code immediately following the End Try statement. Not allowed in Finally blocks.
Finally
Optional. A Finally block is always executed when execution leaves any part of the Try statement.
finallyStatements
Optional. Statement(s) that are executed after all other error processing has occurred.
End Try
Terminates the Try...Catch...Finally structure.
bulletGo To

Branches unconditionally to a specified line within a procedure.

GoTo line

Part

line
Required. Any line label.

 

bulletReturn

Returns control to the code that called a Sub, Function, or Property procedure.

Return 

-or-

Return expr

Part

expr
Required in a Function procedure or a Property procedure that retrieves the property's value. An expression that represents the value to be returned to the calling code.

 

Input/Output

bulletCommand Line: via the console object interfaces.
bulletGUI: via the Visual Studio .Net development environments.  This includes use of windows and standard GUI objects (text boxes, buttons, etc.).
bulletFile I/O: via low-level and system level objects.
bulletDatabase: via ADO, ADO.Net and similar database integration technologies.

Resources

References

bulletGoogle Directory: http://directory.google.com/Top/Computers/Programming/Languages/Visual_Basic/Visual_Basic.NET/
bulletMicrosoft MSDN Reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconProgrammingWithVB.asp

 

[Home] [Language Basics] [C Language] [C++] [C#] [Java] [JavaScript] [Visual Basic 6.0] [VBScript] [VB.Net] [.Net Technology]

Copyright © 2003-2005 by Dave Hillman