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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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