Solved

How to create multiple string array in a Do while Loop

Posted on 2014-01-23
4
172 Views
Last Modified: 2014-02-12
Each time my reader picks up a line I would like to create a new Arrfield to be created

   'Only looks at files marked .CSV in the Filename
        If Me.lblFileName.Text.Contains(".csv") Then
            ' Ready to read the file....
            Do While Not reader.EndOfData
                'Parse the line into fields using gthe ReadFields method
                Dim arrFields As String() = reader.ReadFields()
                Dim strCurrentField As String
                Dim array_counter As Integer = 1
                CSVDataloop = 0
                'Process all Header information and populates in combo boxes
                Dim strCurrentLine As String = String.Empty
                For Each strField As String In arrFields
                    newPoints = New FilePoints
                    'Reads Information in.
                    strCurrentLine += strField + Environment.NewLine
                    'checks to see if File that is opened is a valid File.
                    If Not strCurrentLine.StartsWith("Date") Then
                        If FileCheck_Pass = False Then
                            'MessageBox.Show("This is not a Valid/Accpted File Format. Please select a Valid Format.", "File Open", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                            MessageBox.Show("The Header File has been incorrectly setup in the .CSV File", "Header File Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                            SelectedFile_Arr.Clear()
                            _BadFileFormat = True
                            Exit Sub
                        End If
                    Else
                        If strCurrentLine.StartsWith("Date") Or strCurrentLine.StartsWith("DATE") Then
                            CSVHeaderText = True
                        End If
                        FileCheck_Pass = True
                    End If
                    If CSVHeaderText = True Then
                        If strCurrentLine.StartsWith("Date") Or strCurrentLine.StartsWith("DATE") Then
                            CSVHeaderDate = True
                            CSVHeaderCounterOffset = 0
                        End If
                        If strCurrentLine.StartsWith("Time") Or strCurrentLine.StartsWith("TIME") Then
                            CSVHeaderTime = True
                            CSVHeaderCounterOffset = CSVHeaderCounterOffset + 1
                        End If
                        If strCurrentLine.StartsWith("Collector ID") Then
                            CSVHeaderCollector_ID = True
                            CSVHeaderCounterOffset = CSVHeaderCounterOffset + 1
                        End If
                        If strCurrentLine.StartsWith("Operator") Or strCurrentLine.StartsWith("OPERATOR") Then
                            CSVHeaderOperator = True
                            CSVHeaderCounterOffset = CSVHeaderCounterOffset + 1
                        End If
                        If strCurrentLine.StartsWith("Shift") Or strCurrentLine.StartsWith("SHIFT") Then
                            CSVHeaderShift = True
                            CSVHeaderCounterOffset = CSVHeaderCounterOffset + 1
                        End If
                        If strCurrentLine.StartsWith("Seq") Or strCurrentLine.StartsWith("SEQ") Then
                            CSVHeaderSEQ = True
                            CSVHeaderCounterOffset = CSVHeaderCounterOffset + 1
                        End If
                        If strCurrentLine.StartsWith("Lab") Or strCurrentLine.StartsWith("LAB") Then
                            CSVHeaderLAB = True
                            CSVHeaderCounterOffset = CSVHeaderCounterOffset + 1
                        End If
                        If strCurrentLine.StartsWith("Nominal") Then
                            CSVHeaderNominal = True
                        End If
                        If strCurrentLine.StartsWith("EngLoLimit") Then
                            CSVHeaderEngHI = True
                        End If
                        If strCurrentLine.StartsWith("EngLoLimit") Then
                            CSVHeaderEngLO = True
                        End If
                    End If


                    'Formats Incoming data to remove are extra characters.
                    strCurrentLine = strCurrentLine.Replace(vbCrLf, "")
                    'Organizes the File Header Information into the boxes
                    If CSVHeaderLoop = 1 Then
                        IntHeaderCounter = 0
                        Try
                            'Standard Header information for all RAW Data files from 440/501/600
                            '**********************************************************************************************
                            'PartfileName
                            strCurrentLine = lblFileName.Text
                            boxes(6).Items.Add(strCurrentLine)
                            boxes(6).SelectedIndex = boxes(6).Items.Count - 1
                            'Date and Time
                            If CSVHeaderDate = True And CSVHeaderTime = True Then
                                strCurrentLine = arrFields(0) & " " & arrFields(1)
                                boxes(12).Items.Add(strCurrentLine)
                                boxes(12).SelectedIndex = boxes(12).Items.Count - 1
                                IntHeaderCounter = IntHeaderCounter + 2
                                'CSVHeaderDate = False
                                'CSVHeaderTime = False
                            End If
                            'Collector ID
                            If CSVHeaderCollector_ID = True Then
                                strCurrentLine = arrFields(2)
                                boxes(7).Items.Add(strCurrentLine)
                                boxes(7).SelectedIndex = boxes(7).Items.Count - 1
                                IntHeaderCounter = IntHeaderCounter + 1
                                'CSVHeaderCollector_ID = False
                            End If
                            'Tool Name
                            strCurrentLine = arrFields(2)
                            boxes(8).Items.Add("440/501/600")
                            boxes(8).SelectedIndex = boxes(8).Items.Count - 1

                            '**************************************************************************************************
                            'Operator
                            If CSVHeaderOperator = True Then
                                strCurrentLine = arrFields(IntHeaderCounter)
                                boxes(14).Items.Add(strCurrentLine)
                                boxes(14).SelectedIndex = boxes(14).Items.Count - 1
                                IntHeaderCounter = IntHeaderCounter + 1
                                'CSVHeaderOperator = False
                            Else
                            End If
                            'SEQ
                            If CSVHeaderSEQ = True Then
                                strCurrentLine = arrFields(IntHeaderCounter)
                                boxes(10).Items.Add(strCurrentLine)
                                boxes(10).SelectedIndex = boxes(10).Items.Count - 1
                                IntHeaderCounter = IntHeaderCounter + 1
                                'CSVHeaderSEQ = False
                            Else
                            End If
                            'LAB
                            If CSVHeaderLAB = True Then
                                strCurrentLine = arrFields(IntHeaderCounter)
                                boxes(10).Items.Add(strCurrentLine)
                                boxes(10).SelectedIndex = boxes(10).Items.Count - 1
                                IntHeaderCounter = IntHeaderCounter + 1
                                'CSVHeaderLAB = False
                            Else
                            End If
                            'Rest Header value
                            CSVHeaderLoop = 0
                        Catch ex As Exception
                            MessageBox.Show("Please check the Export Data File Structure. " & vbCrLf & "The File has been improperly configured.", "Header File Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                            _BadFileFormat = True
                            Exit Sub
                        End Try
                    End If
                    'Parse out the File Data Line by Line
                    ' ************************************************************************************************************
                    ' Try
                    'Checks for Alpha Numeric Point Names in Header file.
                    If strCurrentLine.Length = 9 And System.Text.RegularExpressions.Regex.IsMatch(strCurrentLine, "^[a-zA-Z0-9]+$") Then
                        'Search and Parse Data from .LMI File or .txt File.
                        'Changed code to not look for 9 or 10 characters but to simply look for a cahracter that is not a D or starts with L00
                        'If strCurrentLine.Length >= 1 And Not strCurrentLine.StartsWith("D") And Not strCurrentLine.StartsWith("L00") And Not strCurrentLine.StartsWith("####****####") And Not IsNumeric(strCurrentLine) And Not strCurrentLine.Contains("_") Then
                        If strCurrentLine = previous_value Then
                            strCurrentLine = String.Empty
                        Else
                            newPoints.Feature_Description = (strCurrentLine)
                            newPoints.Include_Feature = True
                            previous_value = strCurrentLine
                            ArrayList_AlreadyCreated = True
                            PointList.Add(newPoints)
                            strCurrentLine = String.Empty
                            MAXDataFields = MAXDataFields + 1
                        End If
                    End If
                    'Parse out the Measured Value/Nominal/USL/LSL
                    If IsNumeric(strCurrentLine) And CSVDataloop >= 5 And PointList_Index < MAXDataFields Then
                        If CSVHeaderNominal = True And CSVHeaderEngHI = True And CSVHeaderEngLO = True Then
                            If array_counter = 4 Then
                                newPoints.D_value = strCurrentLine
                                'multiplestrData = New String() {strCurrentLine}
                                PointList.Item(PointList_Index).D_value = newPoints.D_value
                                'Moves to next Row on Grid
                                PointList_Index = PointList_Index + 1
                                'combobox.Items = multiplestrData

                            ElseIf array_counter = 1 Then
                                newPoints.Nominal_Value = strCurrentLine
                                PointList.Item(PointList_Index).Nominal_Value = newPoints.Nominal_Value
                            ElseIf array_counter = 3 Then
                                newPoints.UCL_value = strCurrentLine
                                newPoints.UCL_value = strCurrentLine - PointList.Item(PointList_Index).UCL_value
                                PointList.Item(PointList_Index).UCL_value = newPoints.UCL_value
                            ElseIf array_counter = 2 Then
                                newPoints.LCL_value = strCurrentLine
                                newPoints.LCL_value = strCurrentLine - PointList.Item(PointList_Index).LCL_value
                                PointList.Item(PointList_Index).LCL_value = newPoints.LCL_value
                            End If
                            array_counter = array_counter + 1
                            If CSVHeaderNominal = True And CSVHeaderEngHI = True And CSVHeaderEngLO = True And array_counter = 5 Then
                                array_counter = 1
                            End If
                        ElseIf CSVHeaderNominal = False And CSVHeaderEngHI = False And CSVHeaderEngLO = False Then
                            If array_counter = 1 Then
                                newPoints.D_value = strCurrentLine
                                PointList.Item(PointList_Index).D_value = newPoints.D_value
                                'Moves to next Row on Grid
                                PointList_Index = PointList_Index + 1
                            End If
                            array_counter = array_counter + 1
                            If CSVHeaderNominal = False And CSVHeaderEngHI = False And CSVHeaderEngLO = False And array_counter = 2 Then
                                array_counter = 1
                            End If
                        End If

                    End If
                    CSVDataloop = CSVDataloop + 1
                    strCurrentLine = String.Empty
                Next
                CSVHeaderLoop = CSVHeaderLoop + 1

            Loop
            'Close the reader
            reader.Close()
            'Adds Data Point Characters to Grid
            If PointList.Count = 0 Then
                MessageBox.Show("The .CSV Partfile Data was not correctly collected. Make sure all Characteristic Name are not longer then 10 characters", "Body File Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                SelectedFile_Arr.Clear()
                _BadFileFormat = True
            End If
            AddOrDeleteRowsInPartFile()
            Exit Sub
        End If

Open in new window

0
Comment
Question by:cmdolcet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40
ID: 39804576
This is what you do, because you declare the array Inside of the loop.

Could you explain your problem in other words?
0
 

Author Comment

by:cmdolcet
ID: 39806419
I need to make multiple arrfield each time it loop thought
So first loop I should have arrfield1 and then on the next loop I need arrfield2 and so on
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39806936
One option is to declare a list of string arrays outside of the loop and then add the arrFields to this list in the loop.

If you are trying to keep all the fields of all rows in memory, a better option in my opinion would be to use a datatable.
0
 
LVL 40
ID: 39806994
I do not concur with CodeCruiser that a DataTable would be the best thing to use. Unless you need to sort, filter or do standard database operations on the lot, a DataTable incurs extra useless overhead.

I would go for a collection, with code like the following:

		Dim yourArrays As New Collections.Generic.List(Of String())

		While True
			Dim arrFields As String() = reader.ReadFields()
			yourArrays.Add(arrFields)
			'Do your stuff with arrFields
		End While

		'After that, to get to the data from the 5th field of what you expected to call arrField2
		Dim yourField As String
		yourField = yourArrays(1)(4)

Open in new window

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question