Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create multiple string array in a Do while Loop

Posted on 2014-01-23
4
Medium Priority
?
174 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 2000 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

URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 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