PrasadK
PrasadK

Reputation: 25

Option Strict disallows late binding using IEnumerable

Dim distinctJoints As IEnumerable
distinctJoints = From row In spotsTable.AsEnumerable()
                                        Group row By Key = row("FUNCJOINTCODE") Into Group
                                        Select Key, Group

_evaluatedJointsCount = (From row In spotsTable.AsEnumerable()
                        Group row By Key = row("FUNCJOINTCODE") Into Group
                        Select Key, Group).Count()

'Process each joint
For Each currentJoint In distinctJoints
    Dim currentJointKey As String = currentJoint.Key

For the above code currentJoint.Key is showing error of late binding after option strict is on. Could you please help me out of this.

Upvotes: 1

Views: 487

Answers (1)

Heinzi
Heinzi

Reputation: 172200

First, let me congratulate your for moving your code towards Option Strict On! It might be some work in the beginning, but it pays off in the long run since a lot of errors will be found at compile-time rather than at run-time.

That said, let's look at your problem. Here:

Dim distinctJoints As IEnumerable

you declare distinctJoints as a non-generic IEnumerable. A non-generic IEnumerable returns items of type Object when iterated over. The type Object does not contain a Key method. This is why you get a compile-time error.

Since your LINQ query returns a generic IEnumerable of an anonymous type, the solution is to use type inference instead. Activate Option Infer On (if you have not already done so) in your project properties and let the compiler infer the correct data type:

' Dim distinctJoints As IEnumerable <-- remove this
Dim distinctJoints = From row In spotsTable.AsEnumerable()
                     Group row By Key = row("FUNCJOINTCODE") Into Group
                     Select Key, Group

Upvotes: 1

Related Questions