我正在尝试检索构造函数中没有被请求作为参数的其他足球属性(交叉,运球,整理,控球) - reportID,playerID和评论都是必需的。
有超过20个足球属性,所以我只是将前几个属性包括在内以便于阅读。
报告类:
Public Class Report
Public Sub New(ByVal reportID As Integer, ByVal playerID As Integer, ByVal comments As String)
_ReportID = reportID
_PlayerID = playerID
_Comments = comments
End Sub
Private _ReportID As Integer = 0
Private _PlayerID As Integer = 0
Private _Comments As String = ""
Private _Crossing As Integer = 0
Private _Dribbling As Integer = 0
Private _Finishing As Integer = 0
Private _BallControl As Integer = 0
Public Property ReportID() As Integer
Get
Return _ReportID
End Get
Set(ByVal value As Integer)
_ReportID = value
End Set
End Property
Public Property PlayerID() As Integer
Get
Return _PlayerID
End Get
Set(ByVal value As Integer)
_PlayerID = value
End Set
End Property
Public Property Comments() As String
Get
Return _Comments
End Get
Set(ByVal value As String)
_Comments = value
End Set
End Property
Public Property Crossing() As Integer
Get
Return _Crossing
End Get
Set(ByVal value As Integer)
_Crossing = value
End Set
End Property
Public Property Dribbling() As Integer
Get
Return _Dribbling
End Get
Set(ByVal value As Integer)
_Dribbling = value
End Set
End Property
Public Property Finishing() As Integer
Get
Return _Finishing
End Get
Set(ByVal value As Integer)
_Finishing = value
End Set
End Property
Public Property BallControl() As Integer
Get
Return _BallControl
End Get
Set(ByVal value As Integer)
_BallControl = value
End Set
End Property
End Class
下面我意识到我只是将reportID,playerID和注释添加到我的typeList中,这就是为什么我为其他属性获取全部0的原因。如何访问属性?
检索数据:
Private Function retrieveReport() As List(Of Report)
Dim typeList As New List(Of Report)
Dim Str As String = "SELECT * FROM Report ORDER BY PlayerID"
Try
Using conn As New SqlClient.SqlConnection(DBConnection)
conn.Open()
Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
While drResult.Read
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")))
End While
End Using 'Automatically closes connection
End Using
End Using
Catch ex As Exception
MsgBox("Report Exception: " & ex.Message & vbNewLine & Str)
End Try
Return typeList
End Function
Private Sub setReport()
For Each rpt As Report In retrieveReport()
'*****General Information
UC_Menu_Scout1.txtComments.Text = rpt.Comments
'*****Technical
UC_Menu_Scout1.UcAttributes1.lblXCrossing.Text = rpt.Crossing
UC_Menu_Scout1.UcAttributes1.lblXDribbling.Text = rpt.Dribbling
UC_Menu_Scout1.UcAttributes1.lblXFinishing.Text = rpt.Finishing
UC_Menu_Scout1.UcAttributes1.lblXBallControl.Text = rpt.BallControl
UC_Menu_Scout1.UcAttributes1.lblXPassing.Text = rpt.Passing
UC_Menu_Scout1.UcAttributes1.lblXHeadingAccuracy.Text = rpt.HeadingAccuracy
UC_Menu_Scout1.UcAttributes1.lblXMarking.Text = rpt.Marking
UC_Menu_Scout1.UcAttributes1.lblXTackling.Text = rpt.Tackling
'*****Mental
UC_Menu_Scout1.UcAttributes1.lblXAggression.Text = rpt.Aggression
UC_Menu_Scout1.UcAttributes1.lblXPositioning.Text = rpt.Positioning
UC_Menu_Scout1.UcAttributes1.lblXAnticipation.Text = rpt.Anticipation
UC_Menu_Scout1.UcAttributes1.lblXComposure.Text = rpt.Composure
UC_Menu_Scout1.UcAttributes1.lblXVision.Text = rpt.Vision
UC_Menu_Scout1.UcAttributes1.lblXTeamwork.Text = rpt.Teamwork
UC_Menu_Scout1.UcAttributes1.lblXWorkRate.Text = rpt.WorkRate
'*****Physical
UC_Menu_Scout1.UcAttributes1.lblXPace.Text = rpt.Pace
UC_Menu_Scout1.UcAttributes1.lblXBalance.Text = rpt.Balance
UC_Menu_Scout1.UcAttributes1.lblXJumping.Text = rpt.Jumping
UC_Menu_Scout1.UcAttributes1.lblXStrength.Text = rpt.Strength
UC_Menu_Scout1.UcAttributes1.lblXStamina.Text = rpt.Stamina
Next
End Sub
我想这并不难,所以请各位帮忙!
答案 0 :(得分:3)
要向属性添加值,请使用With
:
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")) With {.BallControl = drResult("BallControl"), .Dribbling = drResult("Dribbling")})
答案 1 :(得分:2)
您需要将未提供的属性值作为参数分配给构造函数,或者可以向构造函数添加参数。试试这个 - 而不是像这样调用构造函数:
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")))
相反,请执行此操作:
dim rep = New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments"))
.Crossing = drResult("Crossing")
'additional property assignments
With rep
End With
typeList.Add(rep)