The Right Side of the Assignment Statement

The value on the right side of the assignment statement can be anything that has a value, including a property, a variable, a constant, a literal, or an expression, such as 2 + 2. The only qualification is that, in the case of a property, it must be capable of being read at runtime, which almost always is the case. Properties often may not be writable at runtime, but they almost always are readable at r untime.

The issue that arises with the right side of the assignment statement is that the property, variable, constant, literal, or expression must be of a data type that can be stored in the variable or property on the left side of the assignment statement. This is not an issue if the data types on the two sides of the assignment statement are identical. However, if they are not, then whether the assignment will be successful depends on (1) whether the value can be converted to the data type of the property or variable and, if so, (2) whether Visual Basic .NET will permit the conversion.

For example, the Form class has a Visible property, having a Boolean data type, which indicates whether or not the form is visible (True) or hidden (False). This property is writable, so you could assign True or False to it, as in:

Me.Visible = False

This assignment clearly will work because both the value and the property have the same data type, Boolean.

The following assignment statement would not work because you cannotconvert the string "Jeff into a Boolean. Instead, you would get an unhandled exception (error) that a cast (conversion) from the string "Jeff" to Boolean is not valid:

Me.Visible = "Jeff'

In contrast, while the following assignment statements also do not use the same data type on both sides of the assignment statement, the value can be converted, or cast, to the Boolean data type of the Visible property:

Me.Visible = 22Me.Visible = "True"

As discussed in Module 4, a non-zero number (such as 22) will be evaluated as True. Similarly, the string representation of True ("True") will be evaluated as True when an assignment is attempted to a Boolean property or variable. Therefore, the remaining issue is whether Visual Basic .NET will permit the conversion. The rules Visual Basic .NET follows in allowing (or not allowing) a conversion are analyzed in the very next section, Type Conversions."

Was this article helpful?

0 0

Post a comment