Click here to Skip to main content
Licence CPOL
First Posted 5 Jan 2006
Views 31,117
Bookmarked 18 times

One Type To Convert Them All

By | 5 Jan 2006 | Article
Have you ever wished if there could be a type that could be converted at least to all primitive types? Then, this article is for you.

Introduction

Have you ever wished if there could be a type that could be converted at least to all primitive types?

  • One type to handle strings.
  • One type to handle numerics.
  • One type to handle date & time.
  • One type to convert them all.

I don't know if this is a good behaviour in clean designs or not, but it was interesting to me to do so. In addition, I encountered using dynamic types in many languages, especially in scripting and string processing languages like Python.

I heard about integrating something like dynamic typing in C# 3.0. I think it is a bad news. Because C# will be no more a type-safe programming language. Microsoft plans to import everything to it, and I think that will be a buzz oriented language and will produce product oriented developers. After all, I wrote this. You may use it in scenarios like propagating values to UI.

All of the primitive types - which implements the IConvertible interface - can be converted to this type and vice versa.

Using the code

Using the code is simple. You can include the two files [available in the download file above] in your project and compile them in the whole project.

Notice that there are special situations where the C# compiler can not retrieve the type of the variable. In such cases, just cast the variable to the target type. Here is a small sample code:

Variable v = 100;
string s = v;
s = "10.8";
v = s;
double d = v;

Interesting huh? Just try it and let me know how it was useful (if it happens :) ).

History

There is a long history about dynamic typing and static types vs. dynamic types. But I think these are different areas and respond to solving different problems. Integrating all of the possible features that are used in various languages and solutions will lead us to spaghetti code again. And this time, at a higher level, even in conceptual levels.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kaveh Shahbazian

Web Developer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

A .NET Developer came from a 'C' and 'C++' background with some experiences in low level programming. In last 2 years I have performed various works in ASP.NET 2.0, .NET compact framework and other .NET things.
I am looking forward for functional programming to be mainstream (Lisp (Scheme), F#, Erlang and Haskell (Clean)). There are two path there I love them both: Lisp path (Power of macros with Scheme, ...) and ML path(Power of ADT and patterns with F#, Haskell, ...) and there are some middle ground too (Nemerle, Scala).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberNatza Mitzi12:37 9 Feb '09  
GeneralNot like Python at all! PinmemberPaddy31189:53 13 Aug '07  
GeneralGenerics PinmemberTodd Morris4:09 31 Jul '07  
GeneralRe: Generics PinmemberKaveh Shahbazian20:54 31 Jul '07  
GeneralMisunderstanding of C# 3.0 Pinmemberjskeet21:29 5 Jan '06  
GeneralRe: Misunderstanding of C# 3.0 PinmemberKaveh Shahbazian21:25 14 Jan '06  
QuestionWhy? Pinmemberaprenot12:08 5 Jan '06  
AnswerRe: Why? PinprotectorMarc Clifton2:37 6 Jan '06  
GeneralRe: Why? Pinmemberaprenot5:00 6 Jan '06  
GeneralRe: Why? PinmemberKaveh Shahbazian18:38 6 Jan '06  
GeneralRe: Why? PinmemberScott S.9:02 11 Jan '06  
GeneralRe: Why? PinmemberKaveh Shahbazian19:14 13 Jan '06  
GeneralRe: Why? PinmemberScott S.2:04 16 Jan '06  
GeneralRe: Why? PinmemberKaveh Shahbazian22:55 16 Jan '06  
GeneralNice PinprotectorMarc Clifton11:17 5 Jan '06  
GeneralRe: Nice PinmemberKaveh Shahbazian18:42 6 Jan '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 5 Jan 2006
Article Copyright 2006 by Kaveh Shahbazian
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid