2014-12-24 63 views
0

我试图检索我的其他足球属性(越过,运球,完成,球控制),作为参数在构造函数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和意见,我的类型串,这就是为什么我让所有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 

我想这并不难,所以任何帮助,将不胜感激请!

+2

属性是什么,从你在说什么完全不同。你指的是属性,你可以通过点击对象来轻松地调用它们。 'myReport.Finishing = 1234'。它与你在'setReport()'中设置标签'Text'属性没有什么不同。 – TyCobb

+0

您还可以使用自动属性去除该代码的* lots *:除非需要限定setter中的值或ReadOnly,否则“公共属性Dribbling()As Integer”是您所需要的全部内容。 VS/VB将提供一个你仍然可以使用的“隐藏”后台字段('_Dribbling')。 – Plutonix

+0

好的,谢谢你的建议! – Nick

回答

3

要值添加到属性,使用With

typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")) With {.BallControl = drResult("BallControl"), .Dribbling = drResult("Dribbling")}) 
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) 
相关问题