Visual Basic 14.0 Features
Visual Basic 14 is the version of Visual Basic that was shipped as part of Visual Studio 2015.
This version was rewritten from scratch in about 1.3 million lines of VB. Many features were added to remove common irritations and to make common coding patterns cleaner.
The version number of Visual Basic went straight from 12 to 14, skipping 13. This was done to keep VB in line with the version numbering of Visual Studio itself.
#Region directive improvements
#Region directive can now be placed inside methods and can even span over methods, classes and modules.
Comments after implicit line continuation
VB 14.0 introduces the ability to add comments after implicit line continuation.
During coding, unexpected errors do arise frequently enough, which requires debugging and testing. But sometimes the errors are indeed expected and to bypass it, there is the
Try..Catch..Throw..Finally..End Try block.
To manage an error correctly, the code is put into a
Try..Catch block, whereby the
Catch, as the name states, will catch all the exceptions that arise in this block.
And in case of exception, we have the possibility to
Throw the error, that is return it to notify the user or manage it internally in the code itself.
Finally part is the final code that, whatever the outcome be, if there is an exception or not, the code will run before going out of the block.
In case we need to pop-out of the clock, there is the
Exit Try statement that can be used. But here also, the code in the
Finally section will be executed before ending.
The syntax is simple;
where only the
End Try is compulsory. The rest can be ignored but as a good practice, do include the
Finally part, even if it would be left blank.
Coming to the exception, there are different type of exception that can be caught. They are ready made exceptions available from the .Net Framework, as below;
|Exception Class||Brief Description|
|System.IO.IOException||Handles I/O errors|
|System.IndexOutOfRangeException||Refers to an array index out of range|
|System.ArrayTypeMismatchException||When type is mismatched with the array type|
|System.NullReferenceException||Handles errors generated from referencing a null object.|
|System.DivideByZeroException||Handles errors generated from dividing a dividend with zero.|
|System.InvalidCastException||Handles errors generated during typecasting.|
|System.OutOfMemoryException||Handles errors generated from insufficient free memory.|
|System.StackOverflowException||Handles errors generated from stack overflow.|
Multiline string literals
VB now allows string literals that split over multiple lines.
NameOf operator resolves namespaces, types, variables and member names at compile time and replaces them with the string equivalent.
One of the use cases:
The old syntax will expose the risk of renaming the variable and leaving the hard-coded string to the wrong value.
NameOf, renaming the variable only will raise a compiler error. This will also allow the renaming tool to rename both with a single effort.
NameOf operator only uses the last component of the reference in the brackets. This is important when handling something like namespaces in the
The operator also uses the name of the reference that is typed in without resolving any name changing imports. For example:
Null conditional operator
To avoid verbose null checking, the
?. operator has been introduced in the language.
The old verbose syntax:
Can be now replaced by the concise:
? operator is particularly powerful when you have a chain of properties. Consider the following:
Normally you would have to write something like this:
But with the
? operator this can be replaced with just:
Partial Modules and Interfaces
Similar to partial classes the new version of Visual Basic is now able to handle partial modules and partial interfaces. The syntax and behaviour is exactly the same as it would be for partial classes.
A partial module example:
And a partial interface:
Just like for partial classes the definitions for the partial modules and interfaces have to be located in the same namespace and the same assembly. This is because the partial parts of the modules and interfaces are merged during the compilation and the compiled assembly does not contain any indication that the original definition of the module or interface was split.
Read-only properties were always possible in VB.NET in this format:
The new version of Visual Basic allows a short hand for the property declaration like so:
The actual implementation that is generated by the compiler is exactly the same for both examples. The new method to write it is just a short hand. The compiler will still generate a private field with the format:
_<PropertyName> to back the read-only property.
This new feature makes the string concatenation more readable. This syntax will be compiled to its equivalent
Without string interpolation:
With string interpolation:
The two lines are equivalent and both get compiled to a call to
String.Format, the brackets can contain any single expression (call to a method, property, a null coalescing operator et cetera).
String Interpolation is the preferred method over
String.Format because it prevents some runtime errors from occurring. Consider the following
This will compile, but will cause a runtime error as the compiler does not check that the number of arguments match the placeholders.