Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
photo of Dan Mabbutt

Dan's Visual Basic Blog

By Dan Mabbutt, About.com Guide to Visual Basic since 2002

Option Infer - Round Three

Tuesday June 10, 2008

Another Sneaky Option Infer Bug

I've been working on an upgrade to one of the tutorials to add information about the new LINQ - Language INtegrated Query - capability in Visual Basic 9 and the .NET Framework 3.5. Only VB.NET 2008 versions has it.

So imagine my dismay when the Microsoft examples and tutorials simply refused to work in my code. To be specific, code similar to this example can be found in more than one Microsoft tutorial:

Dim x = _
    From proc In _
    System.Diagnostics. _
    Process.GetProcesses _
    Select proc.ProcessName
For Each y In x
    Debug.WriteLine(y.ToString)
Next

A specific error is reported by VB.NET. In my existing project, the variable y is "not declared".

In hunting the bug, I finally discovered that a new project worked just fine. The "not declared" error doesn't show up. But adding the code to my existing project did not! I carefully checked namespace references, Imports clauses, everything, and finally discovered that the bug actually starts with the variable x. In a new project, x is an IEnumerable type. In my existing project, it's just a generic object type.

Why is that?

It's the sneaky Option Infer that doesn't get set correctly when a project is migrated from VB.NET 2005 to VB.NET 2008. These articles describe the basic problem:

Option Infer in VB.NET 2008

Option Infer: Round Two

In this case, however, it pretty much makes it impossible to code a lot of LINQ statements because what LINQ calls "implicit typing" doesn't create the variable types that you need unless Option Infer is turned on. To solve the problem, simply change the default in your project properties as shown above.

Comments

June 15, 2008 at 1:24 pm
(1) Tom Garth says:

Hi Dan,

I like your new page layout, and the fact that I can open your examples in new tabs or windows.

I’m not sure that I like the idea of ‘Option Infer’.

It reminds me of an issue that I believe still exists with generic collections, and using a numeric ‘Key’ to identify an Item. It just doesn’t work.

EX:
************
Dim MyClass as New SomeClass ‘ has a single property “Name”
Dim MyClassCollection as New Collection

MyClass.Name = “Tom”
MyClassCollection.Add(MyClass, “28″)

‘ The next line will generate an error

MsgBox(”Name = ” & MyClassCollection(”28″).Name)

***************
In order to use a numeric value as the string value for a ‘Key’, it had to be disguised with a non-numeric character in it. “28″ = “_28″.

It performed as though there were already an ‘Option Infer” action going on behind the scenes.

Thanks for the Sunday reading, and keep up the great work.

Tom

June 16, 2008 at 4:56 pm
(2) Dan Mabbutt says:

Hi backatcha Tom!

Thanks for the nice comments about the site. We’ve been working hard on it for quite a while.

I wasn’t aware of the problem you reported, so I coded up a quick example to see if I could duplicate it. In brief, I couldn’t. It worked for me.

On the other hand, I had to modify the code in your message a bit. Maybe I haven’t hit exactly on what you were trying to illustrate. Here’s the code I wrote.

Public Class Form1
    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click
        ' has a single property "Name"
        Dim MySomeClass As New SomeClass
        Dim MyClassCollection As New Collection
        MySomeClass.Name = "Tom"
        MyClassCollection.Add(MySomeClass, "28")
        ' The next line will generate an error
        MsgBox("Name = " & MyClassCollection("28").Name)
    End Sub
    Public Class SomeClass
        Private m_Name As String
        Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = value
            End Set
        End Property
    End Class
End Class

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss

Community Forum

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.