Bill takes on RSS with VB9

Bill has converted the C# only Orcas Beta 1 RSS Feeder example into VB9.  In his conversion he has illustrated how Linq further reduces the need for iterators.  There are a couple of note worthy points from this example:

  • Try and work out exactly where each of the Linq statements are executed?
    • There are two linq expressions defined in the GetItems method but neither of them get executed until the ToArray method is called.  The ToArray method effectively iterates the expression converting the Ienumerable into an array (this is so that you can iterate the same array multiple times without reiterating/re-executing the linq expression)
  • Take a guess what the new Option Infer On means?
    • This controls whether types are inferred or not.  If this property is set to Off you can’t write Dim x = 5 and assume the compiler will know that x is an integer.

There are of course some weird behaviour to do with this new Option Infer:

Option Explicit – you must declare your variables

Option Strict – you must type your variables

Option Infer – you can infer the type

Ok, so now I’m confused so how about some examples:

Explicit [Off] Strict [Off] Infer [Off]
x = 5

[No compiler checking, x is an Object that is auto-declared essentially where it is first used]

Explicit [On] Strict [Off] Infer [Off]
Dim x = 5

[x is typed as Object since the compiler can only guess what it could be at runtime]

Explicit [On] Strict [On] Infer [Off]
Dim x as Integer = 5

[x is typed as an Integer and the compiler ensures that all references to x do appropriate casts.  With Infer off you can’t use anonymous types which means that all Linq statements have to return predefined types]

Explicit [On] Strict [On] Infer [On]
Dim x = 5

[x is typed as an Integer and the compiler ensures that all references to x do appropriate casts.  Anonymous typing is permitted as the type is inferred by the compiler]

Explicit [On] Strict [Off] Infer [On]
Dim x = 5

[x is typed as an Integer but the compiler allows implied type conversions (eg x = “6”).  Anonymous typing is permitted]

 

My recommendation is to turn Option Explicit and Option Strict on at a project level (as with previous versions of Visual Studio) and that you also turn on and get used to Option Infer as anonymous types truly enable the full power of Linq.

Leave a comment