Click here to Skip to main content
13,290,261 members (54,175 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 16 Dec 2003

Safe typecasting of arbitrary data

, 16 Dec 2003
Rate this:
Please Sign up or sign in to vote.
A library for safely converting data from obscure sources into most commonly used strong types.


Let'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 null reference is ... well, I'm sure you know.

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 System.Convert namespace functions, while great at what they do, are still somewhat rigid to be truly useful, forcing you to write additional checks and exception handlers just to get at the data.


The SafeTypecast library is nothing more than a layer between your data access logic and System.Convert utilities, with several additions. It supports conversions to all standard data types (such as bool, long, int, float, etc.), standard extension types (unsigned longs and ints) and some other common types (DateTime).

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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alex Beynenson
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 17 Dec 2003
Article Copyright 2003 by Alex Beynenson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid