Dynamically working with Enum's

Submitted by Robert MacLean on Sun, 08/05/2007 - 18:31
Enums in .NET are very powerful in defining options. By default when you define an enum it automatically assigns them sequential integer values from 1 (if you don't specify a start value). So how do we work with these dynamically? Well some say you can't, and they are wrong. But first let me cover the basics, if you want to skip over this scroll down to example 6.

Basics Of Enum

Example 1

In this example First would be equal to 1, Second to 2 and Third to 3.
public enum Demo { First, Second, Third }
Example 2

In this example First is equal to 1, Second to 222 and Third by 986.

public enum Demo { First = 1, Second = 222, Third = 986 } 
Example 3

What’s nice is that if you just want to change the start position then you can define that only, so in this example First is 10, Second is 11 and Third is 12.

public enum Demo { First = 10, Second, Third } 
Example 4

Even better is the ability to decorate the enum with the "flag" attribute, set the numbers (Raymond Chen explained why this is not done automatically) and use it as bitflags. Note the integer values are in traditional flag values with None set to 0 and All set to the combined value.

[Flags] 
public enum Demo 
{ 
None = 0, 
First = 1, 
Second = 2, 
Third = 4, 
All = 7 
} 
Example 5

So how do we use those flags? The code below will output:

First, Third

The code is:

static void Main(string[] args) 
{ 
    Demo Enum = Demo.First | Demo.Third; 
    Console.WriteLine(Enum); 
} 


Dynamically Using Flag with Enums

So here we are basics out of the way, and now on to the fun. I continue to use the definition in example 4 above.

Example 6

First I will show how to add a value to the enum variable. What I do is start off by defining the enum to none (0 value) then using the OR concat (|=) symbol I add each enum. This code will output:

Second, Third

The code is:

static void Main(string[] args) 
{ 
    Demo Enum = Demo.None; 
    Enum |= Demo.Second; 
    Enum |= Demo.Third; 
    Console.WriteLine(Enum); 
} 
Example 7

In this last example I will show how to remove an value from the enum variable. I start off by defining all (integer value of 7) and then I use the AND concat (&=) symbol and prefix the enum value with tilde (~). This code will output:

First, Third

The code is

static void Main(string[] args) 
{ 
    Demo Enum = Demo.All; 
    Enum &= ~Demo.Second; 
    Console.WriteLine(Enum); 
} 

This IE add on should be standard

Submitted by Robert MacLean on Wed, 07/11/2007 - 08:01

Bruce Nicholson pointed the other day to an add on for IE call IE 7 PRO (You can get it from http://www.ie7pro.com) which has some great features. His main one to show me was th spell check feature which allows you to spell what you type in web pages. You can see the difference it made to my potjie article Before and After. This is really nice but not my favorite featre.

My favorite is the crash recovery, should IE or Windows crash (which my Vista install does when I sleep it in a hurry) the next time you open IE it prompts you to restore the last session. If you say yes it opens all the tabs to the locations you were at. AMAZING :)

WSUS and Vista

Submitted by Robert MacLean on Thu, 07/05/2007 - 17:01

A while ago I blogged about some fun at being an early adopter, the issue that caused it was that Vista wouldn't update. Funny enough the new stuff didn't help a bit, not even a little bit. What was happening was I was getting that error while not connected to the work network due to my machine being set to get it from the company WSUS server. While on the network I got a different error (8007000b if I remember right)
For love or money I couldn't fix it, until I stumbled on the fact WSUS 3.0 was released. Upgrading the company WSUS server to that fixed the problem. Seems we were using a RTM of 2.0 and Vista support was only added in 2.0 SP 1 :(

Regardless 3.0 is really worth the upgrade. Only problem is now I am getting new patches almost every day until I catch up to all of them.

Anyway grab all the yumy WSUS freshness at http://technet.microsoft.com/en-us/wsus/default.aspx

My first potjie

Submitted by Robert MacLean on Thu, 07/05/2007 - 12:15

This past weekend I braved Makro and the evil hordes which invade it on weekends to buy my first potjie since I had asked some friends over on Sunday for a late lunch. Since I have never made a potjie before I did some searches on line and found a few articles, but in the end decided to just go crazy. From all accounts the food was good so here is the recipe:
Ingredients:
- 1.3kilos of beef, cubed into about half inch sizes
- 3 crazy big carrots, diced into bit size pieces
- 4 large sweet potatoes, cubed in to about half inch sizes
- 2 pundits of button mushrooms, diced
- 2 green peppers diced
- 2 large sweet peppers diced
- A cast of other things I forgot about now and will unveil during the preparation.

Cooking:
Take the meat and added some freshly ground thyme, rosemary, and Robinson's meat tenderizer. Put that in a container and add a couple of dollops of Mrs Balls chuckney, BBQ flavored marinade and Bandito hot sauce (I used the 5 heat level one). Close container, shake and store in fridge turning every so often until needed. Mine sat for about 3 hours but the longer you leave it the better.
Once your potjie is nice and hot add all the veggies plus half a liter of water and leave for about 30min-45min.
Next add the meat and the basting sauce it has been in to the mix and throw in two cubes of beef stock, and two packets (50g each) of tomato paste. Mix well. Close lid and leave for 2 to 3 hours stirring every 15-20min.
Eventually the liquid should be low and you get more of a thick sauce than soup (think of it as a stew almost). If it starts to catch on the bottom it's ready and has been for 10min or so. Move it to a low heat and dish up on rice.

Microsoft is pushing open source

Submitted by Robert MacLean on Fri, 06/15/2007 - 22:52

Before I begin let me clarify a few points so you are aware of the context of this post.
1) I currently work for a Microsoft Gold Partner, been contracted to Microsoft (through my employer) and spoken at TechEd.
2) Before that I worked for company which used almost solely open source software and developed software for that. I also did work with the Shuttleworth foundation at the linux days event.
So I have been on both sides of the fence, now on to the content...

Go anywhere near Slashdot and mention Microsoft and you will get atleast a few zealots who complain about it's attitude/actions to FOSS (Free and Open Source Software).

Well I am not going into the free part, since until I can live without money making everything free doesn't help and there is many people in the traditional OSS community which do make money (RedHat, Novell, many linux contributors etc...) so I guess I am not alone in this view.

But many people bring up the open source side, which makes less sense to me. Microsoft does have this shared source thingy which is some special license for special people, but that doesn't fit with true OSS where anyone can access it so I'm not including it. Microsoft has Port25 (there public front for their internal open source lab) which has some interesting information, but not really pushing OSS. There are also a few "sponsored" projects on SourceForge and the now defunct CodePlex. Neither of these push OSS as part of major projects. So while benefical aren't big enough.

So what is it that doesn't make sense to me? Simple, the .Net framework is completely open source. All of the .Net assemblies are in source code (IL) all the time and thanks to reflection can be transformed into a convient language of choice of the viewer. Since .Net is the big push from Microsoft the new core of their systems will be open source. This can be seen currently with their applications built on top of it, like Microsoft CRM which has all their assemblies in .Net and can all be opened and viewed. Microsoft CCF is even further advanced with it including some applications in .Net (like the admin console) but the bulk is available in direct source code. BTW Neither are obfuscated in anyway, so there is no attempt to close this source.

Agreed that the core big money makers (Windows, Office etc...) are still closed source, but how much of that is legacy versus how much is based on the choice of language tool (C++ Win32 vs Dot Net) and how much of that is specific plans to close off the source I can not say and no one outside the core executive at Microsoft could say, but the argument that Microsoft doesn't get it, is just wrong. They get it and probebly more than most of the zealots mentioned earlier since they have figured out to use it strategically.