Click here to Skip to main content
Click here to Skip to main content

.Net - Use The Framework

, 22 May 2010
Rate this:
Please Sign up or sign in to vote.
Don't reinvent the wheel - use the one that's already on the cart.
I've found that there are many parts of the .Net framework that can be used "on the sly". For instance, there was a "Quick Answer" question posted today that presented this problem:
 
Given a string of configuration settings, like this (with more items than I'm showing here):
 
string text = "screen_fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\"
 
The user asked how to parse the string so he could change the settings in the string. Most of you may have already recognized this string for what it probably is - something from a XML file. There were a couple of responses to this question that suggested using the string.Split() method, or even Regex (wholly inappropriate IMHO), but there is an easier way, and the framework comes to the rescue.
 
Simply make the string into an acceptable XML format by added a name and the necessary brackets so that it takes on the look of an actual XML element, and then it's a simple matter to process the element instead of its string counterpart, like so:
 
// our original string
string text = "screen_fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\"";
// add our XML endcaps
text = string.Format("<TEST {0} />", text);
//Parse the string into an XML element
XElement element = XElement.Parse(text);
// change the value(s) we want to change
element.Attribute("screen_fadetimeout").Value = "5";
// and return our string to its original configuration
text = element.ToString().Replace("<TEST","").Replace("/>","").Trim();
 
My tip is to use the framework where possible instead of reinventing the wheel or doing things the hard way.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberTarek Elqusi31-Jan-13 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 22 May 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid