This is somewhat of a follow up on a previous post I did on the difference between IEnumerable(Of T) and the IEnumerable interfaces. 

I've seen several people type in the following code and wonder if there was a fundamental bug in the type inference code.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each cur In Controls
            cur.Text = "A Value"
        Next
    End Sub

This code will produce an error stating that "Text" is not a member of object.  Users expected type inference to type the variable "cur" as Control.  Unfortunately this is "By Design". 

Much of the original .Net Framework was written before the CLR implemented support for generics.  As a result all of the collection classes were loosely typed to Object by implementing IEnumerable.  So in this case type inference will correctly type this as Object.

There are 2 ways to fix this problem.

  1. Explicitly type the For Each variable to be the actual type of objects in the collection
  2. Use a Shim to change the type of the collection. (see http://blogs.msdn.com/jaredpar/archive/2007/10/04/ienumerable-and-ienumerable-of-t.aspx)