Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

What's New in Visual Basic 9.0? Part 1 : Page 2

The release of Visual Studio 2008 updates Visual Basic to version 9.0, with several key language enhancements and LINQ support. Part one of this series will walk you through using nullable types, type inference, the If operator, and other time-saving enhancements.


advertisement
Figure 1. Default: All unspecified types default to Object.

Type Inference
In VB 8.0, you could declare a variable without specifying its type. In such cases, the variable is of type Object, which means and you needed to perform typecasting during runtime. The downside to this is that IntelliSense wouldn't know the variable type during design time, making it difficult for you to deal with the type (see Figure 1).

VB 9.0 offers a new option: Option Infer. This option is turned on by default. Thus, all variables declared without specifying the type are automatically be inferred by its initializer. Consider the following example:



Dim obj obj = 5 '---obj is Now integer--- obj = "Hello" '---obj is now string--- Dim num = 9 '---obj is integer--- num = "World" '---error---

Here, obj is of type Object because there is no initializer. Hence, during runtime it can take on values of any type. In contrast, num is automatically inferred as type Integer because of the initializer value of num will cause a runtime error. Unfortunately, unless you use the Option Strict On, this error will not be flagged during compile time.


Figure 2. Inferred: Loop variants are also typed inferred.
 
Figure 3. Inferred: The loop variant's type is inferred by the content of the pts array.

Automatic type inferencing also applies to loop variants. Figure 2 shows that, depending on the values assigned to the loop variable i, its type will be automatically inferred.

As another example, consider the code shown in Figure 3. Here, pts is an array of Point objects. The loop variant pt will automatically be inferred to be of type Point, as evident in IntelliSense.

In contrast, if pts contains a mixture of Point and Size objects (see Figure 4), then the pt loop variant will then be inferred to be of type Object.

Figure 4. Inferred: pt is now inferred to be of type Object.

The If Operator
VB 9.0 also allows you to use the if operator to evaluate an expression and conditionally return one of two values. Consider the following example:

Dim num = 20 MsgBox(If(num > 0, "Positive", "Negative"))

In the above statement, if num is greater than 0, the string "Positive" would be displayed, else "Negative" would be displayed. The if operator first evaluates the first argument, which is num>0, and if the result of that expression is true, the second argument would be evaluated and returned. If the result is false, the third argument is evaluated and returned.

The if operator is similar to the IIf() function, with one notable difference: the If operator uses short-circuit evaluation—that is, it only evaluates one of the arguments depending on the result of the first expression. The following example makes this clear:

Dim d = 0 '---prints out 0--- MsgBox(If(d <> 0, 50 \ d, 0)) '---this one will result in an runtime error--- MsgBox(IIf(d <> 0, 50 \ d, 0))

Here, because d is 0, if you use the if operator, only the third argument is evaluated. If you use the Iif() function, both the second and third arguments would be evaluated regardless, resulting in a division by zero error for the second argument (50 \ d).

You can also use the If operator with two arguments. Consider the following example:

Dim conn As SqlConnection = Nothing conn = If(conn, New SqlConnection("Data Source=.\SQLEXPRESS; " & _ "Initial Catalog=pubs;Integrated Security=True")) '---prints out the pubs connection string--- MsgBox(conn.ConnectionString)

Here, if conn (the first argument) evaluates to Nothing, the second argument is returned (which is a SqlConnection object). If conn evaluates to anything other than Nothing, then the first argument (which is conn in this case) is returned. Contrast the above statements with the approach that you'd normally take:

If conn Is Nothing Then conn = New SqlConnection( _ "Data Source=.\SQLEXPRESS;Initial Catalog=pubs; " & _ "Integrated Security=True") End If MsgBox(conn.ConnectionString)

Using the if operator with two arguments makes your code cleaner and easier to read.

In the following example, the Northwind connection string will be printed out instead:

Dim conn As SqlConnection conn = New SqlConnection("Data Source=.\SQLEXPRESS; " & _ "Initial Catalog=Northwind;Integrated Security=True") conn = If(conn, New SqlConnection( _ "Data Source=.\SQLEXPRESS;Initial Catalog=pubs; " & _ "Integrated Security=True")) '---prints out the Northwind connection string--- MsgBox(conn.ConnectionString)



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap