Welcome to Honest Illusion Sign in | Join | Help

Generics without Collections, Pt. 3

Over the weekend, I attended the third NJ Code Camp. And since the moderators asked nicely, I presented this series as a talk.  Overall, the lecture didn't go well. (I was too nervous and talk too quickly.  Oddly, on one of the evaluation sheets, someone complained that I was talking too loudly, which I first is the first time in my life that was ever said about me)

Also, I think I was a victom of scheduling a bit.  My presentation was during the first shift, and was on an advanced topic.  The other generics talk that day (by Kevin Goff), which was much more of an introduction to generics, was given during the last shift.  So, if you didn't know generics well, you'd be completely confused by my talk.  But, if you'd seen Kevin's first, and then saw mine, it would have made more sense.

Anyway, for the presetation I came up with a few more examples, which I'll be posting to the blog in the upcoming days (between Rev. Billy updates --- Remember the day after Thanksgiving is [wikipeida:Buy Nothing Day])

 

One of the attendees to the presentation mentioned that he could use generics to simplify parsing enums, and another said that he'd already done it.  It took me only a few moments to write the code, so I figured I'd share it with you:

Presently, to parse a string into a enum value, you have to write it like this:

   1:  enum ABC {Able, Baker, Charlie};
   2:  string str = "Charlie";
   3:  ABC c = (ABC) Enum.Parse(typeof(ABC), str);

 

No big deal, but it's a bit ugly, and you have to specify the enum name twice, and if you type a mismatch, you get a run-time error.    Let's see how we can make this a bit prettier & safer with generics.

static class Enumr

{
    public T Parse<T>(string value)
    {
        return (T) Enum.Parse(typeof(T), str);
    }
}

Which would be used like this

   1: enum ABC {Able, Baker, Charlie};

   2:  string str = "Charlie";
   3:  ABC c = Enum.Parse<ABC>(str);

 This way is a bit easier to read & type, and a mismatch specifiying the enum names will cause a complie-time error.

Oddly, just this morning I found a link to a page giving a very similar method to convert a int to an enum, namely

   1:  public static E ConvertIntToEnum<E>(int value)
   2:  {
   3:      return (E)System.Enum.ToObject(typeof(E), value);
   4:  }
But that method is completely unecessary. You can convert an int to and enum (and an enum to an int), by simple casting:

ABC a = ABC.Able;
int ai = (int) a;
ABC aa = (ABC) ai;

 

 

kick it on DotNetKicks.com
Share this post: Email it! | bookmark it! | digg it! | reddit!
Readability Stats: Word Count: 469; Sentence Count: 21; Grade Level: 7.6, more info...
Published Monday, November 20, 2006 11:46 AM by James

Comments

# re: Generics without Collections, Pt. 3

James,

I was there on Saturday.  Perhaps you remember me.  I talked to you for a few moments after your presentation.  I've seen Kevin's presentation before, and it doesn't compare to yours.  You didn't waste our time with mundane examples, and you seem to really know what you're talking about.  Now, I just have to figure out how to use some of what you presented.  Thanx.

Dave

BTW, what format is the ZIP file you posted on njcodecamp.org?  I can't open it, and it's not WinZip compatible.  Thanx again.

Monday, November 20, 2006 12:54 PM by Dave

# re: Generics without Collections, Pt. 3

Thanks for you comments. I'm not ready to completely give up. I'm planning on re-tooling the presentation and try it again. Probably not for NJCC4, but perhaps for NJCC5. The Zip file was created with Winzip v9.0, and the copy I have locally opens fine. However, I just download the version that is online at NJCodeCamp.org, and, I can't expand that one either. However, this link appears to work: http://honestillusion.com/files/folders/c-sharp/entry4012.aspx
Monday, November 20, 2006 2:27 PM by James

# re: Generics without Collections, Pt. 3

That did the trick.  I can open the zip file now.

Thanx.

Tuesday, November 21, 2006 9:44 AM by Dave

# http://google.com/reader/view/user/07521674948431533534/state/com.google/starred

Friday, December 22, 2006 8:35 AM by TrackBack

# Generics without Collections, Part 3

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Friday, February 16, 2007 12:03 PM by DotNetKicks.com

# Implementing A Circular Iterator

Many years ago, I wrote an article entitled "Implement A Circular Iterator" for The VisualC++ Developer's

Wednesday, February 28, 2007 11:42 PM by Honest Illusion

# Implementing A Circular Iterator

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Wednesday, February 28, 2007 11:52 PM by DotNetKicks.com

# Implementing A Circular Iterator

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Wednesday, February 28, 2007 11:54 PM by DotNetKicks.com
New Comments to this post are disabled