March 2007 - Posts

A friend was recently writing code that needed to create email attachments based on text in a database, and the only way he could find to create the attachments from the text was to create a text file (see the constructors for the Attachment class). This bothered me, so I ripped out some code that would take the string, and convert it to a stream, so he could call the constructor for the Attachment class that accepts a Stream instance. I thought the code was useful, so I wrote a little article about it for Code Magazine (http://www.code-magazine.com/Article.aspx?quickid=0703121). I hate when I do this. I got an email yesterday, pointing out that again, I should perhaps do some research first:

I work a lot with attachments, and I thought your article was very well written.  However, it was also kind of un-neccesary.  There is a static function in the Attachment class

 

Attachment.CreateAttachmentFromString

 

that I believe would do everything in the article (while also being infinitely easier to maintain since its built in ). 

Ah, well. David A. Cohen sent the email, and he's completely correct. I guess it would have been a better plan to actually examine the members of the Attachment class before writing the code. A word to the wise: If you find yourself writing .NET code which seems like it sure as heck ought to have been included in the Framework, take a few seconds and try to convince yourself that it's not. Especially before you display your laziness in public! Sorry, folks.

Posted by KenG | 2 comment(s)