Click here to Skip to main content
6,634,665 members and growing! (15,769 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Safe typecasting of arbitrary data

By Alex Beynenson

A library for safely converting data from obscure sources into most commonly used strong types.
C#, Windows, .NET 1.0, .NET 1.1, Visual Studio, Dev
Posted:16 Dec 2003
Views:22,175
Bookmarked:7 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.17 Rating: 3.10 out of 5
1 vote, 20.0%
1
1 vote, 20.0%
2

3
2 votes, 40.0%
4
1 vote, 20.0%
5

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:

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

About the Author

Alex Beynenson


Member

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Dec 2003
Editor: Smitha Vijayan
Copyright 2003 by Alex Beynenson
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project