Visual Basic .NET Language

Topics related to Visual Basic .NET Language:

Getting started with Visual Basic .NET Language

Visual Basic .NET is the official successor to Microsoft's original Visual Basic programming language. Visual Basic [.NET] appears to have similarities to Python with the lack of semicolons and brackets, but shares with C++ the basic structure of functions. Curly braces are absent in VB .NET, but instead are replaced with phrases like End If, Next, and End Sub.

Console

Array

Dim myArray(2) As Integer

someFunc(myArray)

An array is an index-ordered collection of objects. The type of object is defined by the type given in the array declaration.

Arrays in Visual Basic .NET are most commonly (and by default) zero (0) based, meaning that the first index is 0. An array of 10 elements will have an index range of 0-9. When accessing array elements, the maximum accessible index is one less than the total number of elements. Because of this, loops that access array indices incrementally should always do a range check where the value is less than the array length.

Visual Basic 14.0 Features

Extension methods

Extension methods are methods (Sub or Function) that add functionality to a Type (which may be a Reference Type or a Value Type). These Types may or may not be owned by you.

They may or may not be in the same assembly as the Type they are intended to modify. You can allow an opt-in to your extension methods by isolating them in their own namespace. Or if you prefer you can make them always available by including them in the same namespace as the Type they modify (assuming all the assembly references are in place and correct). See the Entity Framework Core 1.0 project on GitHub for a good example of the opt-in style of extension methods.

Extension methods in VB have a few requirements:

  • Extension methods may only be declared in modules.
  • Extension methods must be decorated with the Extension() attribute.
  • The ExtensionAttribute namespace must be available within your module.
    Imports System.Runtime.CompilerServices
  • The first parameter to the method must be of a type that this method will be attached to.
  • The first parameter of the method will represent the instance that this method operates on. (Equivalent to Me if this were a real instance method).
  • An extension method can be called as a regular method by supplying all parameters if not called on the instantiated object.

Reflection

Looping

Enum

Threading

Dictionaries

File Handling

Short-Circuiting Operators (AndAlso - OrElse)

'AndAlso' and 'OrElse' are ShortCircuiting operators that means that the execution is shorter because the compiler doesn't evaluate all the expressions in a boolean comparision if the first one provides the desidered result.

Task-based asynchronous pattern

LINQ

Data Access

Disposable objects

Operators

Operators are used to assign or compare values. They consist of a single symbol or keyword and are usually sandwiched between a left and a right value. For example: right = left.

Operators are intrinsic to the language (such as =), and not functions such as those provided by System.Math.

Declaring variables

Classes

Lists

Date

Introduction to Syntax

Option Strict

Option Strict On is a recommended good practice with Visual Basic .Net. It helps you as the developer to produce cleaner, more stable, more bug-free, more maintainable code. In some cases it may also help you write programs with better performance, avoiding things such as Implicit Conversion.

On is not the default setting for a new installation of Visual Studio. It should be one of the first things changed before beginning programming if you are going to use VB.NET. The reason it is not the default setting comes from the first editions of Visual Studio when programmers were expected to be migrating projects from VB6.

NullReferenceException

NullReferenceException is thrown whenever a variable is empty and one of its method/properties are referenced. To avoid this, be sure all variables are initialized correctly (new operator), and all methods returns a non-null value.

FTP server

Error Handling

OOP Keywords

Working with Windows Forms

File/Folder Compression

ByVal and ByRef keywords

Type conversion

Option Explicit

Option Infer

GDI+

Google Maps in a Windows Form

BackgroundWorker

Connection Handling

Using BackgroundWorker

Generics

Multithreading

Unit Testing in VB.NET

This is the simplest yet descriptive example for the unit testing procedure. Feel free to add more methods to check against different data types.

Reading compressed textfile on-the-fly

Conditions

Recursion

Using Statement

WPF XAML Data Binding

WinForms SpellCheckBox

Debugging your application

Functions

Using axWindowsMediaPlayer in VB.Net

Random

Finally, a note about randomization. As mentioned earlier, when you declare an instance of Random without any parameters, the constructor will use the current time as part of the calculation to create the initial seed number. Normally this is OK.

However. If you re-declare new instances over a very short space of time, each time the seed number is calculated, the time could be the same. Consider this code.

For i As Integer = 1 To 100000
    Dim rnd As New Random
    x = rnd.Next
Next

Because computers are very quick these days, this code will take a fraction of a second to run and on several dequential iterations of the loop, the system time will not have changed. So, the seed number will not change and the random number will be the same. If you want to generate lots of random numbers, declare the instance of random outside the loop in this simple example.

Dim rnd As New Random
For i As Integer = 1 To 100000
    x = rnd.Next
Next

The basic rule of thumb is don't re-instantiate random number generator over short periods of time.