title: VB.NET: Reading a CSV file into a 2D array
body: <p>I'm working on a project that requires me to take values from a CSV file. I've to do further processing with these values and it'd be great if I can have these values in a 2D array. The number of rows and columns of the CSV files changes at regular intervals. </p>
  
  <p>I'm unable to take these values into a 2D array in VB.NET/C#. Can I please have some help on that?</p>
  
  <p>Here the code that I used:</p>
  
  <pre><code>Imports System.IO
  
  Public Class Form1
      Private Sub ReadCSVFileToArray()
          Dim strfilename As String
          Dim num_rows As Long
          Dim num_cols As Long
          Dim x As Integer
          Dim y As Integer
          Dim strarray(1, 1) As String
  
          ' Load the file.
          strfilename = "test.csv"
  
          'Check if file exist
          If File.Exists(strfilename) Then
              Dim tmpstream As StreamReader = File.OpenText(strfilename)
              Dim strlines() As String
              Dim strline() As String
  
              strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)
  
              ' Redimension the array.
              num_rows = UBound(strlines)
              strline = strlines(0).Split(",")
              num_cols = UBound(strline)
              ReDim strarray(num_rows, num_cols)
  
              ' Copy the data into the array.
              For x = 0 To num_rows
                  strline = strlines(x).Split(",")
                  For y = 0 To num_cols
                      strarray(x, y) = strline(y)
                  Next
              Next
  
              ' Display the data in textbox
  
              For x = 0 To num_rows
                  For y = 0 To num_cols
                      TextBox1.Text = TextBox1.Text &amp; strarray(x, y) &amp; ","
                  Next
                  TextBox1.Text = TextBox1.Text &amp; Environment.NewLine
              Next
  
          End If
      End Sub
  
  
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          ReadCSVFileToArray()
      End Sub
  End Class
  </code></pre>