VB.NET Tutorials

Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

The VB.NET programming language

The VB.NET language accesses the powerful types in the .NET Framework with its distinctive syntax form. Knowledge of this language helps many developers who primarily use other languages. VB.NET has features nearly equivalent to the C# language. It commands tremendous expressive power.

Array Examples File Handling Regex Articles String Notes Windows Forms

Overview: This section shows example VB programs. It covers VB.NET syntax, keywords and performance.

Console

As an introduction, let's run a program that uses the Console.WriteLine subroutine and prints Hello world to the screen. The program is contained in a module named Module1, and the Sub Main is the entry point of the program.

Console.Write
Program for introduction [VB.NET]

Module Module1
    Sub Main()
	' Say hi in VB.NET.
	Console.WriteLine("Hello world")
    End Sub
End Module

Output

Hello world

Numbers

Steps

Numbers are often stored as Integer types in VB.NET programs. If you have data in a String that you want to convert into an Integer, you must first parse it. We cover various aspects of numbers and mathematical processing in the language. Chars are essentially numbers in VB.NET as well.

Char Examples Integer.Parse Function Examples Integer.TryParse Random Number Example Math.Abs: Absolute Value Math.Max and Math.Min Boolean Use Math.Sqrt DescriptionEnum type

Enums

Enums are an excellent choice to use in your VB.NET code base if you have certain magical constants to embed. They greatly improve the documentation quality because they are always labeled. We describe Enums in the language.

Enum Examples

Convert

Conversion or change

Converting data in the VB.NET language requires a lot of knowledge of what functions are available in the .NET Framework. We elaborate upon conversions in the VB.NET programming language.

Convert ArrayList to Array Convert Char Array to String Convert Dictionary to List Convert List and Array Convert List to String Convert String Array to String Convert String to Integer Convert.ToInt32 Function

Built-in functions and statements. VB.NET also provides a host of useful built-in functions that you can use. These typically provide low-level functionality and conversions.

Chr Function Example CStr Usage Mid Statement Mod Operator Val and Asc Function Examples VarType FunctionROT13 algorithm illustration

ROT13. The ROT13 encoding algorithm is a clever method that shifts the letters in the alphabet by 13 places in two directions based on their values. It is easily reversed. We present a complete implementation in the VB.NET language.

ROT13 Encode Function

Units. It is possible to develop conversion methods in the VB.NET language that change one unit to another unit. Often this can be done with a simple multiplication expression.

Convert Miles to Kilometers

DateTime

The current time (now)

The DateTime type is a special value type in the VB.NET language, and it includes many different functions that you can use. We describe various aspects of the DateTime type and its usage; the TimeSpan type is also covered.

DateString Example DateTime Examples DateTime Format Functions DateTime.Now Property DateTime.Parse Converts String to DateTime TimeSpan Examples

Dictionary

If you have a large collection of elements and want to find one very fast, the Dictionary type presents an ideal solution. Simply perform lookups on the Dictionary instance; you can also use the Hashtable type for a similar effect.

Dictionary Examples Hashtable Basics KeyValuePair Examples Sort Dictionary ToDictionary Method TryGetValue ExampleList type.

List

You often need to store many elements in a resizable array. You might not even know how many elements are needed when you begin processing. The List and ArrayList types are an excellent choice for programs with this requirement—the List type is best.

ArrayList Examples BinarySearch List List Find Function: FindIndex, FindLast List Tips Sort List

Collections

There are other collection types available for use in your VB.NET programs. Some of these, including the HashSet and Tuple types, are generics. They can be used to simplify certain problems.

Set collection HashSet Example Queue Examples Stack Type Tuple Examples

Interface

Interfaces in the VB.NET programming language introduce an essential level of abstraction that you can use to act upon many different types through one Interface type. This represents polymorphism and can help simplify your program.

Interface Example

Syntax

ByVal/ByRef operators in VB.NET

We cover various aspects of VB.NET syntax: the looping constructs, as well as the subroutine syntax. These tutorials may be helpful as an introduction to VB.NET for people familiar with other languages.

AddressOf Operator ByVal Versus ByRef Example Inherits Keyword Is IsNot Operators Lambda Expression Select Case Example Select Case String Shared Function Sub Procedure Example Underscore Line Example With StatementLoop illustrated by arrow

Loops

Here are the looping constructs in the VB.NET language. The For Each construct is probably the least error-prone but is not always available. We also see a simple example of the For-loop construct directly on this page.

Do While Loop For Each Loop Examples For Loop Examples Loop Over String: For and For Each While Loop
Program that demonstrates For loop [VB.NET]

Module Module1
    Sub Main()
	For i As Integer = 0 To 3
	    Console.WriteLine(i)
	Next
    End Sub
End Module

Output

0
1
2
3

Recursion

When a function calls itself, recursion occurs. The VB.NET language supports recursion—the ByRef keyword can be very useful when implementing recursive algorithms.

Recursion Explanation

Data

Note (please read)

One of the top uses for VB.NET programs is to process data stored in databases. You can use many different types in the .NET Framework to accomplish this task. The DataGridView is one of the most popular choices, and works well with the DataTable.

DataGridView Usage DataRow Tips DataSet Examples DataTable Example

HTML

You can write HTML markup in the VB.NET language without dealing with HTML syntax directly: check out HtmlTextWriter here.

HtmlTextWriter Example

Threads

Question and answer

What should you do when you have a long-running task that you don't want to block the main thread on your VB.NET program? You should introduce threads: the BackgroundWorker is a top choice for this.

BackgroundWorker Use Sleep Method ThreadPool Tips

Timing

You can also use timers in your VB.NET programs. Timers are excellent for checking up on your program with a diagnostic method—you can run it every 30 seconds.

Timer Tutorial Stopwatch Example Benchmark ProgramsShell screenshot

Process

How can you invoke an external process, such as an EXE, from a VB.NET program? The easiest way is to call the Process.Start function. Another approach is to use the Shell function. We provide many examples for how you can start processes.

Process.Start Examples Process Kill Example Shell Function

Summary

.NET Framework information

The VB.NET language exposes the power of the .NET Framework to many programmers. With the framework methods, you can avoid writing a lot of tedious code, leading to much faster development and more robust software applications.

Dot Net Perls