Solved

Need assistance with multidimensional arrays in VB.net 2010

Posted on 2013-05-22
5
488 Views
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
                    dReader.ReadLine                    
                 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
                    dReader.ReadLine()                      
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
            dReader.Close()
        End Using
    End Function


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

Thanks!

CJ
0
Comment
Question by:cin_champ
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:SStory
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.
0
 
LVL 25

Expert Comment

by:SStory
ID: 39188952
Also
     line = dReader.ReadLine

                If line.Contains("Spec") Then
                    dReader.ReadLine        

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

Author Comment

by:cin_champ
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?

Cj
0
 
LVL 25

Accepted Solution

by:
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
                    bPrevLineContainsSpec=true
                    dim itemsA() as string = line.Split(",")
                else
                    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

                    bPrevLineContainsSpec=false
                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.
0
 

Author Closing Comment

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



Thanks for your help.
Cj
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now