1.19. Use Implicitly Typed Variables

Problem

You need to create a strongly typed variable without explicitly declaring its type in an effort to save some development time or support LINQ, which is discussed in more detail in Chapter 6.

Solution

Ensure Option Infer is On, and then create a variable and assign it a value without using As and specifying a type.

How It Works

VB .NET 9.0 allows you to create strongly typed variables without explicitly setting their data types. You could do this in previous versions of VB .NET, if Option Strict were set to Off, but the variable was always typed as an Object. In this case, its type is automatically inferred based on its value.

To use this new functionality, Option Infer must be set to On. You can specify ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.