Need assistance with multidimensional arrays in 2010

Posted on 2013-05-22
Last Modified: 2013-05-24
Dear Expert,

I need assistance with multidimensional arrays.

I’m looping though a CSV file to find 2 rows and  fill  an array list with each row's matching  values .I woud like to put them in  the array to be able to write them back into another csv.

Each Spec  Row needs to be concatenate with the matching symbol (field) the label  row..

My Code:
        Dim items As String()
        Dim Datalabel(2) As String
        Dim arrDataLabels As String = Datalabel(0)
        Dim arrData As String = Datalabel(1)

        Using dReader As New IO.StreamReader(filePath)
            Dim line As String
            While dReader.Peek >= 0

                line = dReader.ReadLine

                If line.Contains("Spec") Then
                 items = line.Split(",".ToCharArray())

                    'ReDim Preserve arrDataLabels(0, items.Length())
                    For count = 0 To items.Length() - 1
 ‘ Getting error here ¿ 'Chars' is 'ReadOnly'.
                  -->    arrDataLabels(0) = items(count).ToString
                        count += 1
                    Next count
                End If

                If line.Contains("(%)") Then
items = line.Split(",".ToCharArray())
                    ' ReDim Preserve arrDataLabels2(items.Length())
                    For count = 0 To items.Length() - 1
‘  Getting same error here   ¿ 'Chars' is 'ReadOnly'.
and here---->   arrData(1) = items(count).ToString
                        count += 1
                    Next count
                End If

            End While
        End Using
    End Function

Can you explain what I'm doing wrong and  how I should be inserting the data  into my array ?


Question by:cin_champ
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
  • 3
  • 2
LVL 25

Expert Comment

ID: 39188949
Neither of these are declared as arrays
    Dim arrDataLabels As String = Datalabel(0)
    Dim arrData As String = Datalabel(1)

It should be something like
    Dim arrDataLabels(10) As String = Datalabel(0)
    Dim arrData(10) As String = Datalabel(1)

In other words 10 is the dimensions here.  I assume with arr in front these should be actual arrays. What size dimensions you need, I don't know.

I think I'd just use a collection and be done with it.  Also why are you splitting to a char array and then trying to stuff that into strings?

Can you show a sample of the input and desired storage and output.

You could make a simple class or structure with fields, with a a constructor that takes the expected values as a string array (from the split), puts each item in a member of the class, and overrides ToString() to produce whatever output you want.

Create a collection
Dim MyCollection As New System.Collections.Generic.List(Of YourClassName)

And add each item to it if you like.
LVL 25

Expert Comment

ID: 39188952
     line = dReader.ReadLine

                If line.Contains("Spec") Then

Are you trying to see if line x has Spec and read another line after it?

Author Comment

ID: 39191100
Thanks for the reply,

I need to loop though a file using datareader .writer. The file is small I'm looping thought the files for  first time only to get info for line that contains "Spec"   and need to grad all the values on this line and concatenate them with the matching colum value from the next  Row that contains (%) .

Row  that contains Spec  col1  need to be concatenated with the row  that contains ((%)) col1(X) +  col2 (Y) ...
for as many (COLUMNS) values as there are on those rows...

Any ideas as how I can getthat done?

LVL 25

Accepted Solution

SStory earned 500 total points
ID: 39191959
I have no idea why you are doing this:
items = line.Split(",".ToCharArray())
Why the ToCharArray() instead of just leaving it a string?

     dim bPrevLineContainsSpec as boolean = false
     While dReader.Peek >= 0

                'get the line
                line = dReader.ReadLine

                If line.Contains("Spec") Then
                    dim itemsA() as string = line.Split(",")
                    if bPrevLineContainsSpec then
                         'previous line contained Spec
                          dim itemsB() as string = line.Split(",")
                          'at this point itemsA() is an array holding all values for the comma  
                          'separated line containing SPEC and itemsB() the values for the line
                          'after it
                          'so you can index items 0-N in either as follows
                          'itemsA(0) for the first item of a, etc...

                         'the conditions have been met here so do whatever you need to with it
                    end if

                end if

Open in new window

This sounds like homework and we are only allowed to help so much on that, but I have pointed you in the direction of using a flag to know if the line before had the Spec or not and only one ReadLine statement and storing values for the current and previous lines.

Author Closing Comment

ID: 39194066
Not a school project just not paying atention when I copied code.

Thanks for your help.

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 (…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

687 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