How can I make a 2-dimensional array?

DaJackal

New member
Joined
Oct 10, 2009
Messages
4
Programming Experience
Beginner
Hello!

I want to make a vb.net program that reads from a text file a movie, with the help of a Open Dialog (called openFD in my program) (we don't know how many movie we've got), the actors, the release date, the country, and the rating.

The text file look like this:
Movie1;Actor,Actor,Actor1;Date1;Country1;Rating1
Movie1;Actor,Actor,Actor1;Date2;Country2;Rating2
...

First question: How can I make a 2-dimensional array? line(0,0) - Movie1, ... , line (1,5) - Rating2. Here Is my try, but it's not good:

VB.NET:
Dim line As ArrayList = New ArrayList()
        Dim itm As ArrayList = New ArrayList()
        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim objReader As New System.IO.StreamReader(openFD.FileName)

        Do While objReader.Peek <> -1
            line(i) = objReader.ReadLine() & vbNewLine
            For j = 0 To line(i).ToString.Length()
                itm(i, j) = line(i).ToString.Split(";")
            Next j
            i = i + 1
        Loop

I want to read the textfile, then I wanna put all 'n' movies in ListBox1, and when I click on a movie, the rest of the details to appear like this: In TextBox1, the Title, in TextBox2 - the actors, TextBox3 - the country, in data(datepicker) - the release date, and in vot (numericupdown) - the rating. After that, I want to make 3 buttons: New Movie (adds a movie to the list, with all the details - i am writing the details in the textboxez), Modify (modifies some movie detail), Delete Movie(deletes a movie with all the details). And after that, with the help of a Save Dialog (saveFD in my program), i want to write the things that I changed in the text file, respecting the template of the file.

I will put you down my code (it is a mess, i know), and if you can't help me with the things that i told you, please help me. Thank you!

This is the code for the open tool (with the openFD file dialog):
VB.NET:
Private Sub menuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuOpen.Click
        openFD.Title = "Open a movie database"
        openFD.Filter = "Text Files|*.txt|Database Files|*.dat|All Files|*.*"
        openFD.FileName = "filme"

        Dim film() As String
        Dim TextLine As String
        If (openFD.ShowDialog() = Windows.Forms.DialogResult.OK) Then 
            Dim objReader As New System.IO.StreamReader(openFD.FileName) 

            ListBox1.Items.Clear()

            Do While objReader.Peek() <> -1
                TextLine = objReader.ReadLine() & vbNewLine
                film = TextLine.Split(";") 
                ListBox1.Items.Add(film(0)) 
            Loop
            ListBox1.SetSelected(0, True)
        End If
    End Sub

Here is the code for the save tool file dialog (saveFD)
VB.NET:
Private Sub toolSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toolSave.Click
        saveFD.CreatePrompt = True
        saveFD.OverwritePrompt = True

        saveFD.FileName = "filme"
        saveFD.DefaultExt = "txt"
        saveFD.InitialDirectory = "\"
        saveFD.Filter = "Text File (*.txt)|*.txt|Data (*.dat)|*.dat"

        'Dim fileStream As System.IO.Stream
        'Dim userInput As New System.IO.MemoryStream 

        If (saveFD.ShowDialog() = Windows.Forms.DialogResult.OK) Then

            'fileStream = saveFD.OpenFile()
            'userInput.Position = 0
            'userInput.WriteTo(fileStream)
            'fileStream.Close()
            Dim TargetFile As IO.StreamWriter
            TargetFile = New IO.StreamWriter("C:\test.txt", True)
            'TargetFile.WriteLine(Now())
            TargetFile.Write(TextBox1.Text, ";", TextBox2.Text, ";", data.Value, ";", TextBox3.Text, ";", vot.Value)


        End If

Here is the code for the ListBox1. It is working, but I can't manipulate the details of a movie, that's why I need a working code for a 2-dimensional array
VB.NET:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim Item As String
        Dim TextLine() As String
        Dim camp() As String
        Dim i As Integer = 0
        For Each Item In ListBox1.SelectedItems
            TextBox1.Text = Item.ToString()
            'TextBox2.Text = ListBox1.SelectedIndex()

            Dim objReader As New System.IO.StreamReader(openFD.FileName)
            Do While objReader.Peek() <> -1 'Merge pana nu mai este informatie in text
                TextLine = objReader.ReadToEnd.Split(vbNewLine) 'Variabila TextLine ia valoarea unui rand de text
                camp = TextLine(ListBox1.SelectedIndex()).Split(";") 'Despartim randul de mai sus in fct de ;
                'ListBox1.Items.Add(film(0)) 'Adaugam filmul la ListBox
                'i = i + 1
                'Dim aux() As String = TextLine(ListBox1.SelectedIndex())
                TextBox2.Text = camp(1)
                TextBox3.Text = camp(3)
                data.Value = camp(2)
                vot.Value = camp(4)

            Loop
            'TextLine(ListBox1.SelectedIndex())
        Next
    End Sub

And here is the code for the Delete Button. It deletes a movie from the listbox, but the details of the deleted movie remains for the next one, I must delete the details for the deleted movie, too.
VB.NET:
ListBox1.Items.Remove(ListBox1.SelectedItem.ToString)
 
Is the file structure predefined from somewhere else? If its a structure you can control, I would suggest using an XML file document which would provide a lot of flexibility for all the different types of details each movie can hold.
 
I would suggest you either use a database or an xml file for this (xml files can be opened in notepad as it's just a text file but it gives your data a structure)

I would also suggest you make a class to hold the movie info and have your class override the ToString method. Then when you load the data from the file you create instances of your class and you can add the instance of your class directly in the listbox (the listbox calls tostring automatically, that's why you over ride the tostring so you can have the string representation of your class be the movie's name) then when the user clicks on one you can show the rest of the data from the selecteditem
 
Hi!

I forgot to say that I am a newbie in VB.net. If you could give me an example of what you said, please do it. Thank you very much, anyway for the help.
 
Well here's an example of the class you'd create:
VB.NET:
Friend Class MovieInfo
    Private m_MovieTitle, m_Description As String

    Friend Sub New(ByVal MovieTitle As String, ByVal Description As String)
        m_MovieTitle = MovieTitle
        m_Description = Description
    End Sub

    Friend Property MovieTitle() As String
        Get
            Return m_MovieTitle
        End Get
        Set(ByVal value As String)
            m_MovieTitle = value
        End Set
    End Property

    Friend Property Description() As String
        Get
            Return m_Description
        End Get
        Set(ByVal value As String)
            m_Description = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return m_MovieTitle
    End Function
End Class
Then all you gotta do is use it, when you loop the DataTable from the DB or the XML file just create an instance of it and use the ListBox.Items.Add() to store it in the list.

Then you use the Listbox's SelectedIndexChanged event:
VB.NET:
    Private Sub ListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox.SelectedIndexChanged
        If ListBox.SelectedIndex > -1I Then
            Dim currMovie As MovieInfo = CType(ListBox.Items(ListBox.SelectedIndex), MovieInfo)
            'Use currMovie
        End If
    End Sub
 
Hi!

After some struggles, I made some progress, thank you for helping.

As I told you, I wanted to make a vb.net application that reads from a text file some movies with their details, and put the movies in a list, and their details in some textboxes, and when I click on a movie from the list, the textboxes shows me their details. I made an option to add a new movie to the list, modify, but but now I tried to make a delete button that deletes the movie and its details from my application and it gives me an exception. Please tell me where is the problem. Here is the code for the delete button and for listbox:

VB.NET:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim k As Boolean = True
        Dim i As Integer = listFilme.SelectedIndex

        If i >= 0 Then
            listFilme.Items.RemoveAt(i) ;listFilme is my listbox of movies
            movielist.RemoveAt(i) ;movielist is a List (Of movie)
            textActori.Text = ""  ;the textbox with the actors
            Data.Value = ""    ;the datetimepicker with the release date
            textTara.Text = "" ;the textbox with the Country
            Rating.Value = ""  ;the numericupdown with the ratings
        End If

    End Sub

And the exception is here "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index":
VB.NET:
aux = movielist.Item(listFilme.SelectedIndex)

VB.NET:
Private Sub listFilme_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listFilme.SelectedIndexChanged
        Dim aux As New movie
        aux = movielist.Item(listFilme.SelectedIndex)
        textFilm.Text = aux.gettitle()
        textActori.Text = aux.getactors()
        Data.Value = aux.getdata()
        textTara.Text = aux.getcountry()
        Rating.Value = aux.getrating()
    End Sub

Thanks
 
It sounds like you're trying to store multiple bits of data into rows.

That sounds like a job for a database. You wouldn't need multi-dimensional arrays, you'd just read and write to the database.

If you'd like to know how to do this, PM me.

Or, if this is just for class, then keep doing it with a local variable.
 
Back
Top