An attempt at Enum.TryParse

Posted: May 2, 2010 in C#

It is well known fact that Enum.TryParse does not exist in .net 3.5 although it is available in 4.0

There have been some previous implementations already made here, here and here. I thought I should add my $0.02 into the mix & put out my own implementation, so here goes:

Note: line breaks added for clarity & to fit the layout

public static class Extensions{
    public static bool TryParse<T>(this T enum_class,
         string value, out T result){
         return Extensions.TryParse(enumClass, value, true, 
                 out result);
    }
    public static bool TryParse<T>(this T enum_class,
        string value,
        bool ignoreCase, out T result)
        where T : struct
        // cannot constraint by System.Enum type
    {
        result = default(T);
        var is_converted = false;

        var is_valid_value_for_conversion = new Func<T, 
             object, bool, bool>[]{
            (e, v, i) => e.GetType().IsEnum,
            (e, v, i) => v != null,
            (e, v, i) => e.GetType().IsEnum,
            (e, v, i) => Enum.GetNames(e.GetType()).Any(
                n => String.Compare(n, v.ToString(), i) == 0
                || (v.ToString().Contains(",") 
                     && 
                     v.ToString().ToLower()
                     .Contains(n.ToLower())))
                || Enum.IsDefined(e.GetType(), v)
        };

        if(is_valid_value_for_conversion.All(
             rule => rule(enum_type, value, ignoreCase))){
                    result = (T)Enum.Parse(typeof(T), 
                               value.ToString(), ignoreCase);
                    is_converted = true;
       }
      return is_converted;
} }

This code currently can convert the following:

Enum Converted from
enum SomeEnum { A, B, C }

“A” or “a”
enum SomeEnum : int 
{ A = 1, B = 2, C = 3 }

“A” or “a” or 1
[Flags]
enum SomeEnum {
  A = 1, B = 2, C = 4
}

“A” or “a” or “A, B” or “A, b” or “a, B” or “a, b”, 1

Drop a note if you find this useful.

Ladies & Gentlemen, presenting my first open-source application called WebsiteManager – finally released & ready for you good folks to use!

This version (v1.0) provides a UI to manage the ASP.Net Memberships & roles via a web application using ASP.Net MVC 3.5 (although you could use it on ASP.Net MVC 2.0 also)

I have a couple of useful ideas which I plan to implement in this application during the course of the new few releases.

So hang tight :)

Please feel free to download the latest bits from CodePlex, use it as you see fit. I would truly appreciate some feedback on this effort & open to any new ideas you would like to see put into the future releases.

Again – CodePlex is the place for all this information.

Have fun!

Typically, new developers to ASP.Net MVC get stumped with having to deal with one important fact:  Page.ResolveUrl(...); is missing!!!

Don’t worry or fret – the new way of doing this is:

Url.Content(...);

Some sample usage is as below:

Resolving images:

<img src="<%= Url.Content("~/content/images/myImage.gif") %>" alt="My image" title="My image" />

Resolving JavaScript files:

<script language="javascript" type="text\javascript" src="<%= Url.Content("~/scripts/myscript.js") %>"></script>

Simple as that.

When you refer stylesheet files in your *.Master page, this link is directly reference without needing to use the Url helper as shown above.

So, you can continue refering stylesheet files like:

<link rel="stylesheet" type="text\css" media="screen" href="../content/site.css" />

The MVC framework will resolve the path correctly at run-time.

Hmm.

I finally got the chance to start working on ASP.Net MVC with jQuery & Linq.

My last project was based on the now “old” .Net 2.0  & we went with a complete MS technology stack including their version of AJAX. Adding third party controls to the mix didnot help & only added to the overall complexity of the system. 

MS Ajax – is surely past its sell by date.

When this library was first put out, I was truly excited & hoped it would be all it promised to be. For sometime, I really enjoyed it. But then once I got jQuery-ed, its bye-bye Ms. Ajax. Inspite of all her glitz & glamour, I lost my heart to the pure simplicity of jQuery. 

I have never been a big believer of third party controls – regardless of the claims of the component supporters.

True they save you a load of upfront effort & look cool. Unfortunately, in my experience, the project starts degrading in performance, maintainability & testability the minute a third party control starts appearing in your codebase.

Needless to say, after all that – MVC, jQuery & Linq – utopia!!!

Hello world!

Posted: May 21, 2009 in Uncategorized

Welcome to Virtually Thinking – a blog related to web-based application design & development.