?
Solved

Need assistance with multidimensional arrays in VB.net 2010

Posted on 2013-05-22
5
Medium Priority
?
499 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
[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
  • 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 1500 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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