Output name/values of an Enum

Wednesday, March 28, 2007 Labels: , ,

I took the time a while ago to figure out the best way to output each name and value in an Enum. I just keep using the code and have been looking in recent projects looking for the code to re-use in a new project. So, I thought I’d share the love..

This code will output the name and value of each element in the MyEnum object.


string[] names = Enum.GetNames(typeof(MyEnum));
Array values = Enum.GetValues(typeof(MyEnum));
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(string.Format("{0} = {1}", names[i], values.GetValue(i)));
}


I think Reflection is probably the most overlooked and under-appreciated assembly in the whole .Net framework. Don’t you just love it? 8^)

The Windows Vista UAC

Monday, March 19, 2007 Labels: , ,
Everyone has probably already turned off the UAC, but I hadn't until today. I left those annoying popup dialogs, because I actually like the features it provides; not for me, but for when my kids are using my computer. And, let's be honest, once you had everything installed that you normally use, the popup dialog isn't really that annoying anymore.

Today I found a way to disable the UAC dialogs for only Administrators. That is cool! I haven't seen that done yet, so I tried it out and it works good! You can look here for the instructions to perform this tip, courtesy of HowToGeek.com.


Alternatively, you can create a plain-text file on your computer, name it with a .REG extension and put the following into it.

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"ConsentPromptBehaviorAdmin"=dword:00000000


Then, run that file. This registry key location was also courtesy of HowToGeek.com.

Saving as Drafts instead of Posts

Friday, March 16, 2007
I keep saving my blogs as Drafts on accident instead of Posting them. Sorry about that. I've got about a dozen recently that I'll go through and post them. Some of them are pretty cool!

Windows Vista Command-Prompt from Here

Friday, March 9, 2007 Labels: , ,
I stumbled upon a nice feature of Windows Vista Explorer. If you right-click on a folder in Explorer, it will provide you with a different set of options. One of them is "Open Command Window Here," that opens a DOS prompt with the selected folder as the default path. That is cool, since the Windows XP Command-Prompt Here PowerToy was invaluable.

UPDATED: After looking at it again, I noticed that there is one other additional item that is present when holding the Shift key down: "Copy As Path," which simply copies that folder's complete path into the Clipboard. Cool!

Here's a screen shot:

Visual Studio File Comparison Settings

Wednesday, March 7, 2007 Labels: , , ,
.. In other words, how to set Beyond Compare as your file comparison tool in VSTS.

I finally got tired of the default comparison tool in Visual Studio 2005 Team Edition and went poking around to find out how to set Beyond Compare to the default instead. The default utility (WinDiff, I think?) is extremely irritating after having used BC for so many years now.

So, on the Tools | Options dialog, click on Source Control, then Visual Studio Team Foundation Server. Now, click on the "Configure User Tools..." button. Take a look at this screen shot:



The important things to notice are the Extension and Arguments settings. The Extension specifies which files this viewer configuration will be applied to. While the Arguments are specific to the viewer application itself, in this case Beyond Compare. You can change the extension (for instance, to .cs) to apply that configuration to only those file types.

Compare settings:

Extension: .*
Operation: Compare
Command: C:\Program Files\Beyond Compare 2\BC2.exe
Arguments: %1 %2 /title1=%6 /title2=%7

Merge settings:

Extension: .*
Operation: Merge
Command: C:\Program Files\Beyond Compare 2\BC2.exe
Arguments: %1 %2 /savetarget=%4 /title1=%6 /title2=%7


(Disclaimer: I have no ties whatsoever to Scooter Software, the makers of Beyond Compare.)

Use Reflection to compare Class properties with Enum element names

Tuesday, March 6, 2007 Labels: , ,

The following code demonstrates how to use Reflection to simply your code; making it easier to read and understand, and remove code for potential bugs.

The "normal" way of assigning/comparing Enum elements with class properties. In this example, the code must be changed every time you change the user class or the DataField enum. That's a major pain in the neck!

public class User
{
public string FirstName;
public string LastName;
public string EmailAddress;
public User() { }
}

public enum DataField
{
FirstName,
LastName,
EmailAddress,
}

string value = "";
switch (field.DataField)
{
case DataField.FirstName: value = currentUser.FirstName;
break;
case DataField.LastName: value = currentUser.LastName;
break;
case DataField.EmailAddress: value = currentUser.EmailAddress;
break;
default: value = "";
break;
}

Using the switch statement above, you'd have to modify it whenever you add more properties of your User class..

But, by using the following code, you won't ever have to modify the switch statement because it isn't even needed anymore.

public class User
{
public int PlanType;

public string FirstName;
public string LastName;
public string EmailAddress;

public string Address;
public string City;
public string State;
public string PostalCode;
public string Country;
public string PhoneNumber;

public string CreditCardName;
public string CreditCardType;
public string CreditCardNumber;
public string CreditCardExpirationMonth;
public string CreditCardExpirationYear;

public User() { }
}

public enum DataField
{
Unknown,
PlanType,
FirstName,
LastName,
EmailAddress,
Address,
City,
State,
PostalCode,
Country,
PhoneNumber,
CreditCardName,
CreditCardType,
CreditCardNumber,
CreditCardExpirationMonth,
CreditCardExpirationYear
}

string value = "";
FieldInfo[] properties = currentUser.GetType().GetFields();
for (int i = 0; i < properties.Length; i++)
{
if (properties[i].Name == field.DataField.ToString())
{
Object obj = properties[i].GetValue(currentUser);
if (null != obj)
value = (string)obj;
break;
}
}


Of course, you'll still have to ensure that the DataField enum and the User class properties are kept in sync, but you don't ever have to modify your for loop. That's cool!

Universal Charging Pad

Saturday, March 3, 2007 Labels: ,
I was thinking how nice it would be to get rid of all of my power cables and USB cables that are being used to charge up all of my devices. That would be very cool, indeed!

So, I was thinking why not make a "Universal Charging Pad" that you can simply set your device onto and it would charge up. It works simply enough. Some products have already done similar things, such as my Sonicare toothbrush. But, think how nice it would be to set all of your many devices of various sizes (and voltages) onto a single, think mouse-pad-looking-thing to get charged up. No cables, no mess. That'd be cool!

By the way, the Universal Charging Pad copyright is now for sale.. 8^)
Older Posts Home Newer Posts