0

I have multiple values Separated by commas in a single index of an Array That is One Dimensional i.e

value(0) = (M1,1500KM,0.5$), value(1) = (M2,2400KM,0.75$)

Then I want to put these into a Multi Dimensional Array so that

twoDarr(1,0) = 1500KM , twoDarr(1,1) = 2400KM

And so on What am I Doing Wrong here?

Module Module1
    Sub Main()

        Dim FileName, i As String
        Dim c, e As Integer
        Dim h(7, 3) As String
        FileName = "D:\_Docx\Motorways.csv"

        'read the lines into an array
        Dim lines As String() = System.IO.File.ReadAllLines(FileName)
        Dim Motorway = From line In lines
                       Let data = line.Split(",")
                       Select New With {.MID = data(0),
                             .Length = data(1),
                             .Cars = data(2),
                             .Toll = data(3),
                             .Lanes = data(4)}
        For c = 0 To 8
            For e = 0 To 4
                i = lines(c)
                h(e, c) = i.Split(",")
                Console.WriteLine(h(e, c))
            Next
        Next
    End Sub
End Module

I Tried this Loop but it Returns the Error: BC30311 Visual Basic Value of type 'String()' cannot be converted to 'String'.

For c = 0 To 8
    For e = 0 To 4
        i = lines(c)
        h(e, c) = i.Split(",")
        Console.WriteLine(h(e, c))
    Next
Next
  • It looks like you don't need to use an array: all the data is already in `Motorway`. E.g. `Motorway(1).Length` should give you "2400KM". – Andrew Morton Nov 04 '20 at 13:02
  • Thanks for that, it works but i wanted to ask another quick question , how do i dim these automatically based on the csv file... in this case i have to hard code these. – BrAtUkA Causal Nov 09 '20 at 15:20
  • The easy way is to use a List instead of an array. It will automatically expand to hold new items. Also, instead of an anonymous type for each entry in Motorway, you could declare a Class. Oh, and it should be `line.Split(","c)` as there isn't any overload of String.Split which takes a single string as its argument - if you use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) then Visual Studio can tell you about things like that. – Andrew Morton Nov 09 '20 at 16:12

0 Answers0