Home > Software design >  unable to add items to a list in VB.NET
unable to add items to a list in VB.NET

Time:07-20

Good morning, so i have received this homework for the summer where i have to create a program to store a list of movies and display them, but the problem is that there isn't a defined number of movie so i can't use the constant method i've always used, so i tried doing that with variables instead, but whenever i press the input button twice the app crashes and i get the error "Index over the matrix limits" Here's the code in the module

Module Module1
    Public Structure Film
        Public Titolo As String
        Public Autore As String
        Public Incasso As Integer
        Public Nazionalita As String
    End Structure

    Public i As Integer = 0
    Public Flm(i) As Film
End Module

And here's the input part

Public Class frmInput
    Private Sub btnInserisci_Click(sender As Object, e As EventArgs) Handles btnInserisci.Click
        If IsNumeric(txtIncasso.Text) = False Then
            MsgBox("L'incasso deve essere un valore numerico", MsgBoxStyle.Exclamation, "Attenzione")
        ElseIf txtTitolo.Text = "" Or txtAutore.Text = "" Or txtNazionalita.Text = "" Then
            MsgBox("Uno o più valori sono vuoti", MsgBoxStyle.Exclamation, "Attenzione")
        Else
            Flm(i).Titolo = txtTitolo.Text
            Flm(i).Autore = txtAutore.Text
            Flm(i).Incasso = txtIncasso.Text
            Flm(i).Nazionalita = txtNazionalita.Text
            i  = 1
        End If
    End Sub
End Class

CodePudding user response:

You should use a List(Of Film) to store the inputs received.
A generic List like that has no practical limits and can grow while you add elements to it

Public Flm As List(Of Film) = new List(Of Film)
....

Else
    Dim f as Film = new Film()
    f.Titolo = txtTitolo.Text
    f.Autore = txtAutore.Text
    f.Incasso = txtIncasso.Text
    f.Nazionalita = txtNazionalita.Text
    Flm.Add(f)
End If

A List(Of Film) could be used like it was an array

For x As Integer = 0 To Flm.Count -1 Step 1
    Console.WriteLine("Film #" & x 1)
    Console.WriteLine("Titolo = " & Flm(x).Titolo)
    .....
Next

And of course you can iterate over it using a simpler foreach

For Each Film f in Flm
    Console.WriteLine("Film #" & x 1)
    Console.WriteLine("Titolo = " & f.Titolo)
    .....
Next

CodePudding user response:

Although others have mentioned using List, which would probably be appropriate, you also mentioned it was a homework task, so maybe you need, or have to, use the more traditional arrays as you have shown. Bearing this in mind, and also to let you know what your problem is. You are incrementing i but not the array.

Public i As Integer = 0
Public Flm(i) As Film

Thus, Flm is 0 to 0, one element.

You add to this, all is OK.

You increment i, good, i = 1

However, you don't then increment the array, Flm(). Incrementing i doesn't automatically increment the array, Flm().

You need to use: ReDim Preserve

Thus... Change:

Else
Flm(i).Titolo = txtTitolo.Text

to:

Else
ReDim Preserve Flm(i)
Flm(i).Titolo = txtTitolo.Text

Lastly, IsNumeric and MsgBox are remnants of the VB6 days, there are vb.net equivalents. Also, using i as a global/public variable is really not good. It's very common, standard, to use i in all local subroutines and functions for little loops etc, it gets used and lost. If you look at almost all examples of code, you'll see i being used as the integer counter.

  • Related