简体   繁体   中英

VB .Net List of object in list of object

I have a program that marries two objects in a list of objects which meet the conditions.

My problem is the second list of married object doesn't display correctly and I don't know if I'm doing anything wrong.

Here is my code. I have this class with properties, etc:

Public Class Gallo
    Dim taquilla As Integer
    Dim libras As Integer
    Dim onzas As Integer
    Dim puya As Integer
    Dim tuerto As Boolean
    Dim puntos As Decimal
    Dim excluir(3) As Integer

With this class I compare those values and if two of them meet the conditions I want to add these two objects to another class called Married



Public Class Casados
    Dim casado1 As Gallo
    Dim casado2 As Gallo

    Public Sub New(casado1 As Gallo, casado2 As Gallo)
        Me.casado1 = casado1
        Me.casado2 = casado2
    End Sub

    Public Property casado1P
        Get
            Return casado1
        End Get
        Set(value)
            casado1 = value
        End Set
    End Property

    Public Property casado2P
        Get
            Return casado2
        End Get
        Set(value)
            casado2 = value
        End Set
    End Property
End Class

I make a list of that class called casados

Private misCasados As New List(Of Casados)

Then when two objects meet the conditions I add to that list and show them in a datagridview.

                If diferenciaTotal < 2 And diferenciaTotal > -2 Then
                    Dim casados As Casados
                    casados = New Casados(misGallos(i), misGallos(j))
                    misCasados.Add(casados)
                End If
            End If
        Next j
    End If
Next i

dataCasados.DataSource = ""
dataCasados.DataSource = misCasados
For i = 0 To misCasados.Count - 1
    Console.WriteLine()
    Console.WriteLine(misCasados(i))
Next

The problem is that the information displayed in datagridview is just Gallos.Gallo (names of class) not the information I want. I want to show what objects married and show the id Taquilla to know who they are.

You should configure DataGridView to display properties of Gallo class through Casados class.

DataGridView binding recognise only properties (public fields will not be displayed), for example class Gallo on it own can not be displayed, unless you convert all fields into a properties.

Option Strict On ' This is "must have"

Public Class Casados
    Private _first As Gallo
    Private _second As Gallo

    Public Sub New(first As Gallo, second As Gallo)
        _first = first
        _second = second
    End Sub

    Public Property TaquillaOfFirst As Integer
        Get
            Return _first.taquilla
        End Get
        Set(value As Integer)
            _first.taquilla = value
        End Set
    End Property

    Public Property TaquillaOfSecond As Integer
        Get
            Return _second.taquilla
        End Get
        Set(value As Integer)
            _second.taquilla = value
        End Set
    End Property
End Class

Configure columns of DataGridView to display properties TaquillaOfFirst and TaquillaOfSecond

Notice that line dataCasados.DataSource = "" is redundant, feel free to remove it.

Not related but

If diferenciaTotal < 2 AndAlso diferenciaTotal > -2 Then
    Dim casados As New Casados(misGallos(i), misGallos(j))
    misCasados.Add(casados)
End If

When you set Option Strict to On you will start benefit from compiler help, which will recognise possible type errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM