Click here to Skip to main content
11,583,859 members (46,390 online)
Click here to Skip to main content

Tagged as

.Net - Use The Framework

, 22 May 2010 CPOL 18K 5
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)

Share

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.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Tarek Elqusi31-Jan-13 13:09
memberTarek Elqusi31-Jan-13 13:09 
QuestionMy 4: In general I agree... Pin
Andreas Gieriet3-Jun-12 21:44
memberAndreas Gieriet3-Jun-12 21:44 
GeneralWhy you think Rexex is inappropiate? - I'm no fan of it - ju... Pin
johannesnestler24-Feb-12 0:54
memberjohannesnestler24-Feb-12 0:54 
GeneralReason for my vote of 5 nice one Pin
nikhi _singh23-Feb-12 18:11
membernikhi _singh23-Feb-12 18:11 
GeneralReason for my vote of 5 considerable advice Pin
Pranit Kothari8-Jan-12 4:23
memberPranit Kothari8-Jan-12 4:23 
GeneralReason for my vote of 5 My 5! Pretty Logical. This also gave... Pin
RaisKazi1-Nov-11 20:51
memberRaisKazi1-Nov-11 20:51 
GeneralReason for my vote of 5 Learning the Framework is more effic... Pin
Eddy Vluggen16-Sep-11 6:58
memberEddy Vluggen16-Sep-11 6:58 
GeneralI agree Pin
PIEBALDconsult27-Jan-12 8:28
memberPIEBALDconsult27-Jan-12 8:28 
GeneralSo right! Pin
johannesnestler25-May-10 5:01
memberjohannesnestler25-May-10 5:01 
GeneralRe: So right! Pin
John Simmons / outlaw programmer26-May-10 11:27
memberJohn Simmons / outlaw programmer26-May-10 11:27 
GeneralMy vote of 2 Pin
ahmed zahmed 6-Apr-10 12:45
member ahmed zahmed 6-Apr-10 12:45 
GeneralRe: My vote of 2 Pin
John Simmons / outlaw programmer6-Apr-10 23:34
memberJohn Simmons / outlaw programmer6-Apr-10 23:34 
Generala better way Pin
ahmed zahmed 6-Apr-10 12:44
member ahmed zahmed 6-Apr-10 12:44 
GeneralRe: a better way Pin
John Simmons / outlaw programmer6-Apr-10 23:32
memberJohn Simmons / outlaw programmer6-Apr-10 23:32 
GeneralRe: a better way Pin
ahmed zahmed 7-Apr-10 6:06
member ahmed zahmed 7-Apr-10 6:06 
GeneralRe: a better way Pin
John Simmons / outlaw programmer7-Apr-10 23:27
memberJohn Simmons / outlaw programmer7-Apr-10 23:27 
GeneralRe: a better way Pin
ahmed zahmed 22-Apr-10 4:09
member ahmed zahmed 22-Apr-10 4:09 
GeneralGood tip but... Pin
kornman0013-Jan-10 3:27
memberkornman0013-Jan-10 3:27 
Good tip but perhaps try to profile this compared to Split() or the Regex classes?

Depending on how complex the settings string becomes, and how often it is parsed (read: if multiple setting sections use the same format) speed could out weigh this wheel.

You don't want to jump to any conclusions about using other parts of the framework to avoid "reinventing the wheel" when that wheel may have been optimized for a different vessel all together (or not really optimized at all). The ToString operation, or this entire tip (while again, pretty cool), could end up being expensive.

You could also mix it up and use Regex to extract the text inside the TEXT element.
GeneralRe: Good tip but... Pin
John Simmons / outlaw programmer13-Jan-10 3:45
memberJohn Simmons / outlaw programmer13-Jan-10 3:45 
GeneralRe: Good tip but... Pin
kornman0013-Jan-10 5:13
memberkornman0013-Jan-10 5:13 
GeneralRe: Good tip but... Pin
Luc Pattyn17-Jan-10 3:45
mvpLuc Pattyn17-Jan-10 3:45 

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 | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 22 May 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid