Simple Visual Studio 2005 regular expression to swap variable assignments

Tuesday, July 21, 2009 Labels: , ,
Here is a simple regular expression to use in Visual Studio 2005 to swap the variable/object and assignment.
Find What: mySettings.{.+} = {.+};
Replace With: \2 = mySettings.\1;

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^)

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!

Returning dynamic Xml from C# web services

Tuesday, August 1, 2006 Labels: , ,
A co-worker and I spent a while trying to figure out how to return custom XML content from a C# web service. We were trying to figure out how to get our content assigned into a struct (that was being returned) without it encoding our XML tags. It wasn't going to happen. Then he thought of a great idea. He set an XmlNode as the return object. So, we built our XML content in memory as before and simply returned that object. Doing this we able to return any XML data without having to know what it will be before hand.
[WebMethod]
public XmlNode GetDynamicXml()
{
XmlDocument myXmlDocument = new XmlDocument();
// load xml content or build it up dynamically, etc
return myXmlDocument.DocumentElement;
}
I thought it was a clever use of the technology considering the limited alternatives..
Older Posts