簡體   English   中英

如何在構造函數之外訪問類屬性

[英]How to access class properties outside the constructor

我正在嘗試檢索構造函數中未作為參數要求的其他足球屬性(穿越,盤帶,完成,控球),所有這些都是必需的。

足球有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

在下面,我意識到我只是在我的typeList中添加了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

我想這並不難,所以請您提供任何幫助!

要將值添加到屬性,請使用With

typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")) With {.BallControl = drResult("BallControl"), .Dribbling = drResult("Dribbling")})

您需要將未提供給構造函數的屬性值分配為參數,或者可以將參數添加到構造函數。 試試看-而不是像這樣調用構造函數:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM