Answered by:
Option Strict ON disallows late binding

Question
-
I'm very new to LINQ and I'm having issues with the code below. I have blue squiggly line under v("booSelect") and v("ItemCategory"). How do I fix this?
Private dv As DataView Private clm As DataGridViewColumn Dim SelectedValues = (From v In Me.dv.Table.Rows Where v("booSelect") = True And v("ItemCategory") = CInt(UniqueValuesFinder.ItemCategory.UniqueValues) Select v(Me.clm.DataPropertyName)).ToArray
Thanks,
Ryan
Friday, July 20, 2012 5:00 PM
Answers
-
Two reasons:
The rows collection does not IEnumerable(Of T), only IEnumerable. That means, the type of v is Object, not DataRow. Solution:
From v In Me.dv.Table.Rows.Cast(Of DataRow)
Second problem: the expression
v("booSelect") = True
is invalid because v("booSelect") returns an object. You can not compare every object to a Boolean value. Solution:
Where DirectCast(v("booSelect"), Boolean) = True AndAlso ....
Same problem (and analog solution) you will have with the other comparison.
Armin
Friday, July 20, 2012 6:50 PM -
Glad it works now. Just one more suggestion: If you know that the object is of type Boolean, you should use DirectCast instead of CBool. Why? CBool hides an error if you accidently either expected the wrong type (Boolean) or the object has the wrong type (<> Boolean). If CBool can convert to Boolean, you will not see the error immediatelly. DirectCast would throw an exception so you can examine what went wrong.
Armin
- Marked as answer by Ryan0827 Monday, July 23, 2012 2:45 PM
Monday, July 23, 2012 2:41 PM
All replies
-
Maybe this will help http://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx
You've taught me everything I know but not everything you know.
Friday, July 20, 2012 5:48 PM -
Two reasons:
The rows collection does not IEnumerable(Of T), only IEnumerable. That means, the type of v is Object, not DataRow. Solution:
From v In Me.dv.Table.Rows.Cast(Of DataRow)
Second problem: the expression
v("booSelect") = True
is invalid because v("booSelect") returns an object. You can not compare every object to a Boolean value. Solution:
Where DirectCast(v("booSelect"), Boolean) = True AndAlso ....
Same problem (and analog solution) you will have with the other comparison.
Armin
Friday, July 20, 2012 6:50 PM -
Armin,
Thank you for the explanation and the corrected code. You the man!
This is my corrected code.
Dim SelectedValues = (From v In Me.dv.Table.Rows.Cast(Of DataRow)() Where CBool(v("booSelect")) = True AndAlso CInt(v("ItemCategory")) = CInt(UniqueValuesFinder.ItemCategory.UniqueValues) Select v(Me.clm.DataPropertyName)).ToArray
Thanks!
Ryan
Monday, July 23, 2012 2:11 PM -
Glad it works now. Just one more suggestion: If you know that the object is of type Boolean, you should use DirectCast instead of CBool. Why? CBool hides an error if you accidently either expected the wrong type (Boolean) or the object has the wrong type (<> Boolean). If CBool can convert to Boolean, you will not see the error immediatelly. DirectCast would throw an exception so you can examine what went wrong.
Armin
- Marked as answer by Ryan0827 Monday, July 23, 2012 2:45 PM
Monday, July 23, 2012 2:41 PM -
Even better!
Thanks,
Ryan
Monday, July 23, 2012 2:45 PM