|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionLet's face it: strong-typing, while a great idea, can be a pain in the neck when trying to quickly access data that might be coming from not-so strongly typed environments. Introducing the concept of a null adds additional problems in an object-oriented language, where every thing is an object, and trying to do anything with a Most of the time, you want the value to be in a certain form, or if it's not in that form, you need to know that, to deal with it accordingly. Other times, you simply don't care about the data if it's not in the format you expect. The OverviewThe Most of the conversion functions also feature an equivalent overload that allows you to specify a value you would like to be returned, in case the actual conversions fails for any reason. Below is a sample usage example: using ulement.Utilities;
/* retrieve some data from any source (say, database in this case) */
OleDbDataReader rdr = cmd.ExecuteReader();
if (rdr.Read()) {
/* Retrieves a long value. Will return -1 if conversion fails */
long entry_id = SafeTypecast.ToLong(rdr["EntryID"]);
/* Retrieves a long value. Will return -99 (as specified) if conversion fails */
long author_id = SafeTypecast.ToLong(rdr["UserID"], -99);
/* Retrieves a date/time value. Will return DateTime.Now, if fails */
DateTime datetimeposted = SafeTypecast.ToDateTime(rdr["DateTimePosted"]);
/* Retrieves a plain string. Will return a blank string, if fails */
string message = SafeTypecast.ToString(rdr["Message"]);
/* Retrieves a boolean value. Returns false if conversion fails */
bool is_active = SafeTypecast.ToBoolean(rdr["IsActive"]);
}
That's about it, really. Nothing mind-blowing, but definitely a good time and sanity saver. Comments, questions and suggestions are always welcome.
|
||||||||||||||||||||||