I'd like to see if anyone has any suggestions on making the following function faster or more efficient. This code segment reads data from a CVS file, breaks it up and writes the relevant data to a flat file. Some sample input would be:
Column A Column B
004-005 5
35 6
010-034 5
VB Code:
Private Function createzonesFedEx(ByVal Filename As String, ByVal ShipType As String, ByVal Carrier As String) Dim dash As Boolean 'determines whether or not the first column of numbers contain a dash Dim i As Integer Dim line As String 'line of data to be read Dim commaArray() As String 'array of strings delimited by commas Dim dashArray() As String ' array of strings delimited by dashes Dim sr = New System.IO.StreamReader(Filename) ' I/O object to read data from Filename Dim sw = New System.IO.StreamWriter(FILEPATH, True) 'I/O object to write data to FILEPATH line = sr.readline While Not line Is Nothing commaArray = Split(line, ",") If (Mid(commaArray(0), 4, 1) = "-") Then dashArray = Split(commaArray(LBound(commaArray)), "-") dash = False Else dash = True End If For i = 0 To 7 Step 1 Me.cboShipType.SelectedIndex = i Try sw.write(FRFIRM) sw.write(",") sw.write(Me.cboCarrier.Text) sw.write(",") sw.write(Me.cboShipType.Text) sw.write(",") sw.write(FVRSN)'Global constant sw.write(",") If (dash = False) Then sw.write(Format(Val(Trim(dashArray(0))), "000")) sw.write(",") sw.write(Format(Val(Trim(dashArray(1))), "000")) sw.write(",") sw.write(Format(Val(Trim(dashArray(1))), "000")) Else sw.write(Format(Val(Trim(commaArray(0))), "000")) sw.write(",") sw.write(Format(Val(Trim(commaArray(0))), "000")) sw.write(",") sw.write(Format(Val(Trim(commaArray(0))), "000")) End If sw.write(",") sw.write(FRSTA)'Global constant sw.write(",") sw.write(Now.Today.ToString("yyyyMMdd")) sw.write(",") sw.write(Now.Today.ToString("yyyyMMdd")) sw.write(",") sw.write(FRZAEN)'Global constant sw.write(",") sw.write(FRUSER)'Global constant sw.write(",") sw.writeline(FRWSID)'Global constant sw.newline() Catch ex As Exception MessageBox.Show(ex.Message) End Try Next line = sr.readline End While Me.cboShipType.SelectedIndex = -1 sw.Close() End Function
This works fine, I just think it could be done better. Any suggestions? Thank you for your time.




Reply With Quote