Saturday, September 6, 2008

Visual Basic Blog

. Saturday, September 6, 2008

This isn't only about writi ng interesting code. In the case above, the compiler does work that would ordinarily be done every time the code is executed. If this code were in a loop, the CPU savings could be significant.

In the back of my mind, I had the idea that Join was just simple concatenation, like the & operator. In fact, it only works on arrays of strings. This makes it ideal for creating a CSV file - Comma Separated Values, like those commonly used as input to Excel and other programs. (For those who haven't discovered the joys of XML yet.)

I've previously written about how handy the Split function is in the article, Amazing Splits. But I recently discovered that the companion function, Join can be pretty useful too.


To make it just a bit more challenging, I wrote a program that joins an array of strings into a single string in proper CSV file format and writes it to a sequential file - in just one statement.

Quick ... Don't look ... see if you can write just one statement that does all that.

Wanna look? Here's mine.

My.Computer.FileSystem.WriteAllText( _
My.Computer.FileSystem.CurrentDirectory & _
"\CSVFile.txt", _
"""" & Join(ArrayOfStrings, """,""") & """", _
False)

0 comments:

 
Namablogkamu is proudly powered by Blogger.com | Template by o-om.com