Welcome to the Milos Blog!

Saturday, December 20, 2008
Object casting convenience methods

This upcoming version of Milos has a nice little enhancement in the utilities namespace. The DataHelper class now has extension methods that make it very convenient to cast from object types (such as fields in DataSets) to their real types.

These helper methods are implemented as extension methods. To use them, you have to add this line of code at the top of your program:

using EPS.Utilities;

Now, the extension methods are automatically available on all objects. For instance, you can do the following:

DataSet myDs = this.GetSomeDataSet(); // Assume that we somehow get a DataSet
DataRow row = myDs.Tables[0].Rows[0];
Guid myGuid = row["id"].ToGuidSafe();
string myString = row["name"].ToStringSafe().Trim();
int myInt = row["number"].ToIntegerSafe();

The main advantage here is that these methods always return valid values, even if the cast object is not valid. For instance, these methods are aware of System.DBNull and they also trap errors.

There are a number of different such methods, including the ability to cast to booleans, DateTime, decimal, and so forth (all common value types, basically).



Posted @ 2:03 PM by Egger, Markus (markus@code-magazine.com)


Post a Comment:

Comment Title (required):

Your Name (optional):

Your Email (optional):

Your Web Site (optional):

Your Comment (required):


Archive:
November, 2010 (1)
July, 2009 (2)
February, 2009 (2)
January, 2009 (1)
December, 2008 (1)
October, 2008 (3)
June, 2008 (1)
April, 2008 (3)
February, 2008 (1)
January, 2008 (1)
December, 2007 (1)
October, 2007 (2)
September, 2007 (2)
July, 2007 (1)
June, 2007 (2)
May, 2007 (4)
April, 2007 (6)
March, 2007 (8)
February, 2007 (3)
January, 2007 (2)
December, 2006 (2)
November, 2006 (2)


Blog Stats:
Posts: 51
RSS Syndication

Contact:

EPS Software Corp.
6605 Cypresswood Dr., Suite 300
Spring, TX 77379
United States

Phone: 832-717-4445
Email: info@MilosSolutionPlatform.com

Other EPS Properties

EPS Software Homepage
EPS Custom Software
CoDe Magazine
CoDe Focus Magazine
VFPConversion.com
WPFExperts.com
MarkusEgger.com

(c) by EPS Software Corp. 2001 - 2013