Click here to Skip to main content
15,893,722 members
Articles / Programming Languages / C#
Article

Safe typecasting of arbitrary data

Rate me:
Please Sign up or sign in to vote.
3.80/5 (6 votes)
16 Dec 20031 min read 28.8K   308   8  
A library for safely converting data from obscure sources into most commonly used strong types.

Introduction

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.

Overview

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:

C#
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.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --