Typed DataSet Column Casting

  • Thread starter Thread starter J055
  • Start date Start date
J

J055

Hi

I have a created a typed DataSet (VS 2005). A couple of columns return sql
tinyint types which ideally need to be cast to predefined enums for use
throughout the application. It seems that it would be good to do this in a
partial class of the typed DataTable. I would also need to convert the enum
back to the tinyint before inserting/updating the database.

What is the best/cleanest way of doing this? An example would be really
great because I don't know if I have to use an event or override a property.

Many thanks
Andrew
 
Hello J055,

The best way that I've seen it done is to create a new property in the partial
class that does the cast in and out (don't use any additional storage, just
convert in and out). For example (CompanyType is the enum, _CompanyType
is the field name):

public CompanyType CompanyType
{
get
{
try
{
return ((CompanyType)(this[this.tableCustomers._CompanyTypeColumn]));
}
catch (System.InvalidCastException e)
{
throw new System.Data.StrongTypingException("The value for column
\'CompanyType\' in table \'Customers\' is DBNull.", e);
}
}
set
{
this[this.tableCustomers._CompanyTypeColumn] = (int) value;
}
}


Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
http://adoguy.com
 
Back
Top