我有一个程序,可以将满足条件的对象列表中的两个对象结合在一起。
我的问题是第二个已婚对象列表显示不正确,并且我不知道自己做错了什么。
这是我的代码。我有带有属性等的此类:
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
在这个类中,我比较那些值,如果它们中的两个满足条件,我想将这两个对象添加到另一个名为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
我列出了一个名为casados
的课程
Private misCasados As New List(Of Casados)
然后,当两个对象满足条件时,我将其添加到该列表中并在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
问题在于,datagridview中显示的信息只是Gallos.Gallo
(类名)而不是我想要的信息。我想显示已婚对象,并显示ID Taquilla
以了解它们是谁。
答案 0 :(得分:0)
您应该配置DataGridView以显示Gallo
类到Casados
类的属性。
DataGridView
绑定仅识别属性(不会显示公共字段),例如,除非将所有字段都转换为属性,否则无法显示类Gallo
本身。
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
配置DataGridView
的列以显示属性TaquillaOfFirst
和TaquillaOfSecond
请注意,行dataCasados.DataSource = ""
是多余的,请随时删除它。
无关,但
If diferenciaTotal < 2 AndAlso diferenciaTotal > -2 Then
Dim casados As New Casados(misGallos(i), misGallos(j))
misCasados.Add(casados)
End If
将Option Strict
设置为On
时,您将开始受益于编译器帮助,该帮助将识别可能的类型错误。