Click here to Skip to main content
15,898,035 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralALWAYS USE A STRINGBUILDER! [modified] Pin
PIEBALDconsult1-Nov-10 17:49
mvePIEBALDconsult1-Nov-10 17:49 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
fjdiewornncalwe2-Nov-10 1:30
professionalfjdiewornncalwe2-Nov-10 1:30 
GeneralRe: ALWAYS USE A STRINGBUILDER! PinPopular
Samuel Cragg2-Nov-10 2:21
Samuel Cragg2-Nov-10 2:21 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
fjdiewornncalwe2-Nov-10 2:38
professionalfjdiewornncalwe2-Nov-10 2:38 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
AspDotNetDev2-Nov-10 13:28
protectorAspDotNetDev2-Nov-10 13:28 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
AspDotNetDev2-Nov-10 13:40
protectorAspDotNetDev2-Nov-10 13:40 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult2-Nov-10 15:26
mvePIEBALDconsult2-Nov-10 15:26 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
Robert Rohde3-Nov-10 6:32
Robert Rohde3-Nov-10 6:32 
PIEBALDconsult wrote:

If you were writing a data to a fixed-width text file, what technique would you choose?


I would definitely pass a TextWriter into that function. This way the caller could pass either a StringWriter (which uses a StringBuilder internally) or a StreamWriter depending on the target.

Together with some helper functions it would look like this:
public string GetAsText() 
{
   using (var writer = new StringWriter())
   {
      WriteTo(writer);
      return writer.ToString();
   }
}

public void WriteToFile(string path)
{
   using (var writer = File.CreateText(path))
   {
      WriteTo(writer);
   }
}

public void WriteTo(TextWriter writer)
{
   foreach ( DataRow dr in datatable.Rows ) 
   {
      writer.WriteLine(...);
   }
}

GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult3-Nov-10 16:57
mvePIEBALDconsult3-Nov-10 16:57 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
richard_k5-Nov-10 19:18
richard_k5-Nov-10 19:18 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
Member 968-Nov-10 6:25
Member 968-Nov-10 6:25 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult8-Nov-10 13:55
mvePIEBALDconsult8-Nov-10 13:55 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
Member 968-Nov-10 16:13
Member 968-Nov-10 16:13 
GeneralRe: ALWAYS USE A STRINGBUILDER! Pin
PIEBALDconsult9-Nov-10 2:05
mvePIEBALDconsult9-Nov-10 2:05 
GeneralRange Checking Pin
AspDotNetDev31-Oct-10 12:25
protectorAspDotNetDev31-Oct-10 12:25 
GeneralRe: Range Checking Pin
fjdiewornncalwe31-Oct-10 16:02
professionalfjdiewornncalwe31-Oct-10 16:02 
GeneralRe: Range Checking Pin
AspDotNetDev1-Nov-10 6:19
protectorAspDotNetDev1-Nov-10 6:19 
GeneralRe: Range Checking Pin
fjdiewornncalwe2-Nov-10 1:33
professionalfjdiewornncalwe2-Nov-10 1:33 
GeneralRe: Range Checking Pin
BillW338-Nov-10 6:46
professionalBillW338-Nov-10 6:46 
GeneralRe: Range Checking Pin
makumazan8431-Oct-10 23:41
makumazan8431-Oct-10 23:41 
GeneralRe: Range Checking Pin
fjdiewornncalwe1-Nov-10 2:59
professionalfjdiewornncalwe1-Nov-10 2:59 
GeneralRe: Range Checking Pin
AspDotNetDev1-Nov-10 6:20
protectorAspDotNetDev1-Nov-10 6:20 
JokeRe: Range Checking Pin
fjdiewornncalwe2-Nov-10 1:34
professionalfjdiewornncalwe2-Nov-10 1:34 
GeneralRe: Range Checking Pin
richard_k5-Nov-10 19:24
richard_k5-Nov-10 19:24 
GeneralRe: Range Checking Pin
Chris Meech1-Nov-10 5:03
Chris Meech1-Nov-10 5:03 

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

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