tags:

views:

67

answers:

2

I have recently started .NET programming, and looked at both VB.NET and C#. In VB.NET, a strongly typed Datatable cosisted of a collection of strongly types rows. Therefore, for example, this statement would work:

lCustomerTable As CustomerDataSet.CustomerTable 
lCustomerRow as CustomerDataSet.CustomerTable.CustomerRow

lCustomerTable = TableAdapter.GetData
lCustomerRow = lCustomerTable.Rows(0)

However in C#, it seems i have to explicitly cast the returned Row to a CustomerRow:

lCustomerRow = (CustomerDataSet.CustomerTable.CustomerRow)lCustomerTable.Rows[0]

Is there any reason for this? Should the dataset not create the object type definitions when creating the table adapters and SQL dataTables?

+2  A: 

I don't think Rows is actually strongly typed, even for typed datasets. The difference is that VB, by default, permits implicit downcasts. E.g. you can write:

Dim o As Object = ""
Dim s As String
s = o  ' implicit downcast from Object to String!

This is only disabled if you use Option Strict - try that in VB and see if your code still compiles...

In C#, the downcasts must always be explicit - it doesn't have a "non-strict mode".

Pavel Minaev
A: 

[EDIT] I think the answer is to actually cast the returned datarow:

lCustomerRow = (CustomerDataSet.CustomerRow)lMyCustomer.Rows[i];

Pity, since i would have thought the Dataset.xsd should contain this casting information.

Simon
So how did you solve the issue then? As a reference for future visitors.
Leonardo
No Pavel is correct. VB permits a non-strict mode but C# doesn't.
Simon