Jack Jackson
5/24/2008 1:52:00 AM
On Fri, 23 May 2008 20:42:27 +0200, Jeroen Mostert
<jmostert@xs4all.nl> wrote:
>Jack Jackson wrote:
>> I am a little confused by DataView.
>>
>> The Item property returns a DataRowView.
>>
>> However, the object returned by the enumerator used by For Each is
>> apparently Object since I can specify any kind of object and don't get
>> a compiler error:
>>
>> Dim dv as DataView
>> For Each xx As Integer In dv
>>
>> Is the declared type of the item returned by the enumerator used by
>> For Each documented anywhere?
>>
>> Why is it not declared as DataRowView?
>
>DataView only implements the non-generic IEnumerable interface, which
>returns Object. For Each allows you to declare the iterating variable as any
>type and will silently insert casts (which may fail at runtime). Prior to
>.NET 2.0 (which introduced generics) this was the only way to do
>enumeration. .NET 2.0 introduced IEnumerable(Of T) which allows for strongly
>typed enumerations, but DataView dates from before this.
>
>The items in DataView really are DataRowView objects, the interface just
>doesn't express that.
Thanks for the explanation. This makes me think that it is best to
not use a construct like For Each for non-generic classes in order to
get better compile time error checking.