Understanding Implicit Conversions

Previously we discussed the System.Object class. As you might remember, this class is the root in the class hierarchy. That said, you can assign both reference types and value types to an Object instance because they both inherit from System.Object. Consider the following lines of code:

Dim anInt As Object = 10
Dim onePerson As Object = New Person

The first line assigns a value type (Integer) to an Object, and the second one assigns an instance of the Person class to an Object. Visual Basic 2015 always enables such assignments because they are always safe. However, it is unsafe to try to assign an Object to a strongly typed instance, such as an instance of the Person class. This is quite obvious because Object can represent any type, and the compiler cannot be sure if that type is a Person, which can cause errors. Consider the following line of code:

Dim onePerson As Person = New Object

This code is trying to assign an instance of the Object class to an instance of Person. The Visual Basic compiler can handle such situations in two different ways, depending on how Option Strict (which is discussed in Chapter 2) is set. If Option Strict is set to On, the preceding line of code causes an error. The Visual Basic compiler does not allow an implicit conversion from Object to a strongly typed object, and it reports an error message that you can see in the code editor, as shown in Figure 4.7.

Image

FIGURE 4.7 With Option Strict set to On, the Visual Basic compiler disallows implicit conversions.

This is useful because it prevents type conversion errors. If you want to perform an assignment of this kind, you need to explicitly convert Object into the appropriate data type. You can do this, for example, by using the CType conversion operator, as in the following line of code:

Dim onePerson As Person = CType(New Object, Person)

A conversion operator provides another advantage: It communicates whether the conversion is possible; if it’s not, you can handle the situation, particularly at runtime. The code editor also simplifies the addition of a CType conversion by offering a convenient correction pop-up that you enable by hovering the red squiggle that appears under the bad object and then clicking the light bulb symbol. You then see possible fixes, including an option to perform the conversion into the appropriate type (see Figure 4.8 for an example). Note that CType will work without throwing an exception only if the target type for the conversion is the correct type.

Image

FIGURE 4.8 Using the error correction options to easily add CType.


Fixing Code Issues with the Light Bulb

Visual Studio 2015 introduces a brand-new way of fixing code issues via the light bulb shown in Figure 4.8. You will use the light bulb many times to fix code issues. The light bulb is discussed in more detail in Chapter 6, “Errors, Exceptions, and Code Refactoring.”


By the way, the Visual Basic compiler provides a way to allow implicit conversions and avoid error messages. To accomplish this, you need to set Option Strict to Off. You can write the following line of code before all the other code and Imports directives:

Option Strict Off

You can also adjust Option Strict settings in the Compiler tab of the My Project window, as discussed in Chapter 2. Assigning an Object to a Person, as you did earlier, is perfectly legal. But please be careful: If you do not need to perform such assignments, avoid Option Strict Off and instead use Option Strict On. This can ensure fewer runtime and compile-time errors and enable you to write more efficient code.


Option Strict Off: When?

You should almost never set Option Strict to Off. There is only one situation in which you should do this: with late binding. This topic is discussed in Chapter 44, “Reflection.” Outside this particular scenario, you should never set Option Strict to Off so that you can be sure that you are working with strongly typed objects and that the compiler, debugger, and CLR will enable you to quickly find errors. The other reason you should not set Option Strict to Off is that if you do, performance will suffer. You’ll learn about this and other reasons in Chapter 14. I suggest that you set Option Strict to On as a default in the Visual Studio 2015 options.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.135.80