January 2009 Archives

Automatically Generating Dynamic Enumerations

| No Comments | No TrackBacks

I use a product from Developer Express called XtraGrid.  It has some pretty remarkable features, one of which is a “flags” editor.

Say for instance that I have a bit flag enumeration:

[Serializable]
[Flags]
public enum ColorFlags {
    Red = 1,
    Green = 2,
    Blue = 4
}

Now, if I want that to show up in my grid with a popup for a field like the following:

image

I have to do something very simple..

repositoryItemCheckedComboBox_colors.SetFlags(typeof(ColorFlags));

The DevExpress tools will enumerate over the enumeration (ha!) and gather all of the appropriate values, create the checked list edit, and display it to the user.  When the user finishes their edit, they are left with the numerical value of the bitwise operation.

That’s all and well, but what if my enumerations are stored in a database ?  I simply can’t use the .SetFlags method by itself, because there is no hard-coded enumeration to pull from!

What I needed to do was to create the enumeration dynamically, at runtime, to pull the information from the database and construct a dynamic enum.

Here is the code I created:

[Serializable]
public enum FlagsType {
    Color = 1,
    Size = 2
}
private Type GenerateEnumerations(FlagsType f) {
    string asmNameString = String.Empty;
    switch(f) {
        case FlagsType.Color:
            asmNameString = "flags_color";
            break;
        case FlagsType.Size:
            asmNameString = "flags_size";
            break;
    }
    //    Create Base Assembly Objects
    AppDomain appDomain = AppDomain.CurrentDomain;
    AssemblyName asmName = new AssemblyName(asmNameString);
    AssemblyBuilder asmBuilder = appDomain.
DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run); // Create Module and Enumeration Builder Objects ModuleBuilder modBuilder = asmBuilder.
DefineDynamicModule(asmNameString + "_module"); EnumBuilder enumBuilder = modBuilder.
DefineEnum(asmNameString, TypeAttributes.Public, typeof(int)); // Query the database for the value of the flags enumerations using (lsDataContext dbObj = new lsDataContext()) { var enumerations = (from j in dbObj.flags_mainTable where j.type == (int)f orderby j.name select j).ToList(); foreach (flags_mainTable fmtObj in enumerations) enumBuilder.DefineLiteral(fmtObj.name, fmtObj.flag); } return enumBuilder.CreateType(); }

 

Now I simply use the .SetFlags method, but this time like this:

repoItemCheckedCombo_color.SetFlags(
GenerateEnumerations(FlagsType.Color)
);
 

I doubt this sort of scenario crops up often, but if it does I

hope this helps!

  

Matthew MacSuga

OMG WOW!

| No Comments | No TrackBacks

Today, I finally achieved one of my many little dreams as a software developer.

Throughout the years, I have wanted one thing.. one tiny, small, insignificant little thing that would make things just a little easier on me.. allow me to have some fun.. be completely legal [not that I wasn’t already!].. and have access to some of the greatest software tools and systems known to man.

I got my MSDN Premium Subscription today with Visual Studio Team Suite !!

I’ve been downloading all of the neatest applications all day, and planning how, what, and where I am going to install things to make the perfect development environment!  I can’t believe after all of these years of wishing someone would buy it for me.. of wishing I had access to the latest operating systems.. today, I finally have it all!  I absolutely can’t wait to dig into all of the features of VSTS Edition! 

So dear reader, you might ask, how did I get this subscription ?  Did I steal it ?  Did I kill someone ?  Did I lie, or cheat ?  Did I bribe someone at Microsoft ?  No.

Microsoft has this incredible program called BizSpark

With BizSpark, a startup company can acquire all of the tools necessary to develop software using the best Microsoft tools, applications, and operating systems.  The requirements are not steep (less than 3 years, make less than $1,000,000 dollars, be privately held, and be developing software that forms the core of your business) and all you have to do is find a Network partner to help you join the program.  *Note:  The network partner should not charge you for anything, so if someone wants too.. find someone else!  Oh, and one other thing.  You may remain in the program for 3 years, and upon your exit, you are required to pay a $100 exit fee.  Cake.

All in all, I highly recommend this program to anyone looking to have the best development tools that Microsoft has to offer.  I can only assume that I will also get Visual Studio 2010 Team Suite Edition as well, when it’s released.  Let me just tell you something, I absolutely can not wait.  You also get PRODUCTION licenses for SQL Server, and Windows 2008 Server [up to 3 years].

Oh, and did I mention these licenses are perpetual ?  Even after you leave the program, your licenses are valid and will remain so.. with the exception that the server products (SQL Server, Server 2008, etc.) will no longer be licensed. 

All I have to say is this:  Thank you Microsoft, for making my little dream come true. 

I am so happy to finally have access to all of their tools and operating systems.  I am going to have so much fun learning all of the new features in VSTS, and Team Foundation Server.  I can only hope that this will increase my output and make me an even better developer. 

Sounds like fun to me. :)

P.S.  Original Blog Article That I Read to Find Out About This:  http://vistadb.net/blog/news/hats-off-to-47-hats-and-microsoft-bizspark/
Thanks Jason[, and Bob!]!!!

Matthew MacSuga

Core Programming Exercises

| No Comments | No TrackBacks

During the course of researching something.. I came across an excellent free e-book:  “Data Structures and Algorithms” (http://dotnetslackers.com/projects/Data-Structures-And-Algorithms/) written by Granville Barnett and Luca Del Tongo.  While it is a little difficult to follow in places, it is an excellent resource for programmers who want to “get back to basics”.

It’s been a long time since I’ve written things like Linked Lists and Sorting algorithms from scratch. This book really does a good job of laying out pseduo code for what you need to do to implement a particular algorithm. 

Programming in C# has really made me lazy when it comes down to fundamental programming principals!  I would be willing to bed that a lot of programmers today don’t know how to write a lot of these things from scratch anymore.. so much is provided to us to use by others.. that we forget how they’re actually built. 

Using the .NET framework I have hardly had a need to do anything like write a Bubble Sort or a Linked List from scratch.  I suppose that I’m fortunate as well in that I started on the .NET bandwagon late.. a lot of the functionality that is in this book is built into the framework now.

I’m currently working through each of the examples and having quite a bit of fun doing it.  I created my own Singly and Doubly Linked List, from scratch.  While not as powerful or efficient perhaps as the built in Linked Lists and others that are built into the framework.. It’s about learning something old, and applying it to something new. I dug up some old C code that I had written years and years ago that implemented a LL.. now I’ve done it in C# as well.

So, I’m working on getting back to basics.. relearning stuff that I learned a long time ago, and having fun implementing the algorithms in C#.  It’s good exercise for the mind and is making me re-think some of the things that I’ve been writing lately.  Creating these in C# is really making me think more in the OOP world.

Check out the book!

- Matthew 

About this Archive

This page is an archive of entries from January 2009 listed from newest to oldest.

December 2008 is the previous archive.

February 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.