Faster way to add an item to a list, using a structure

Asked

Viewed 98 times

4

Structure turma
    Public id_turma As Integer
    Public nome_turma As String
End Structure

Structure Disciplina
    Public id_disciplinas As Integer
    Public nome_disciplina As Integer
End Structure

Public Class F_Contentor
    Dim turmas As New List(Of turma)
    Dim Disciplinas As New List(Of Disciplina)

 Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    turmas.id_turma.add(1)
End Sub

I have this code. And give this error:

Error 1 'id_turma' is not a Member of 'System.Collections.Generic.List(Of Work_mod_16.turma)'. C: Users Alunop Desktop Work module 16 Work mod 16 Work mod 16 F_container.Vb 50 9 Work mod 16

What is the correct way to add an item to a list, using a structure?

2 answers

3


Switches to:

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    turmas.add(New turma() With { .id_turma = 1 })
End Sub

I put in the Github for future reference.

You need to add an item, and assign a value to it. See the method syntax Add().

Ideally it would be better to use names closer than is customary with language. See recommendations for C# which are very similar to VB.NET. You might also think about creating a constructor for the structure.

  • Thank you for your help. I will do that. : ) (an aside): For this case, it would be an asset to use a class or to continue with the structures?

  • In the meantime an answer has appeared that answers my question :p. Again, thank you.

  • It depends on what you are going to do. By the size I would have the same structure. But you have to analyze other factors, for example the immutability. See more about the differences.

  • 1

    Where do you have an answer that answers? You cannot answer without knowing the case.

  • The one given by Deivid Ramon. But I’ll still read the topics you gave me. Thank you.

  • Yes you’re right.

Show 1 more comment

2

You need to start creating a Class, do not recommend using Structure for this type of problem. You can make a list of structures, but it’s simpler and more functional to use a class. Structures are Valuetypes, when vc uses something like structure(0) = value, vc is creating a copy of that value, anyway, unless you have extreme need or restriction to use a strucure, use a class.

Create the class

Public Class Turma
Public Property IdTurma() As Integer
    Get
        Return m_IdTurma
    End Get
    Set
        m_IdTurma = Value
    End Set
End Property
Private m_IdTurma As Integer
Public Property NomeTurma() As String
    Get
        Return m_NomeTurma
    End Get
    Set
        m_NomeTurma = Value
    End Set
End Property
Private m_NomeTurma As String
End Class

And then establish a List of that class.

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lstTurma As New List(Of Turma)()

lstTurma.Add(New Turma() With { _
    Key .IdTurma = 1, _
    Key .NomeTurma = "Turma do Barulho" _
})
End Sub

To add items to a list, just use the Add method, where you will add new objects from that class.

  • Thanks for the help and quite complete reply. I will probably change the code to use classes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.