Tuesday, May 20, 2008

How do typed data sets differ from untyped data sets, and what are the advantages of typed data sets?

Typed data sets use explicit names and data types for their members, whereas untyped data sets use collections to refer to their members. The following examples show a typed reference vs. an untyped reference to a data item:

Visual Basic .NET
' Typed reference to the Contacts table's HomePhone column.
DataSet1.Contacts.HomePhoneColumn.Caption = "@Home"
' Untyped reference to the Contacts table's HomePhone column.
DataSet1.Tables("Contacts").Columns("HomePhone").Caption = "@Home"
Visual C#
// Typed reference to the Contacts table's HomePhone column.
DataSet1.Contacts.HomePhoneColumn.Caption = "@Home";
// Untyped reference to the Contacts table's HomePhone column.
DataSet1.Tables["Contacts"].Columns["HomePhone"].Caption = "@Home";
Typed data sets do error checking at design time. This error checking helps catch typos and type mismatch errors, which would be detected only at run time with untyped data sets.

No comments: