VB.Net中ReadOnly属性/函数的最佳实践是什么?

Xst*_*ity 0 vb.net properties function visual-studio-2008

我对VB.Net和语法不太新,但我不是专家.我正在用VB重写一些以前在C#中的东西,当我这样做的时候,我遇到了一个"困境".我可以有一个ReadOnly属性:

Public ReadOnly Property MaximumIndenture()
    Get
        Try
            'If the connection is closed, open it.'
            If _dbConnection.State = ConnectionState.Closed Then
                _dbConnection.Open()
            End If

            Dim dictFigureIndenture As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
            Using dbReader As IDataReader = ExecuteReader("SELECT PART_FIGURE, MAX(PART_INDENTURE) AS 'MAX_INDENT' FROM PARTS GROUP BY PART_FIGURE")
                While dbReader.Read()
                    dictFigureIndenture.Add(dbReader("PART_FIGURE").ToString(), Convert.ToInt32(dbReader("MAX_INDENT").ToString()))
                End While
            End Using

            'If the connection is open, do what you need to and/or close it.'
            If _dbConnection.State = ConnectionState.Open Then
                _dbConnection.Close()
            End If

            Return dictFigureIndenture
        Catch ex As Exception
            'An exception was thrown.  Show it to the user so they can report it.'
            MessageBox.Show(ex.Message)

            'If the connection is open, do what you need to and/or close it.'
            If _dbConnection.State = ConnectionState.Open Then
                _dbConnection.Close()
            End If

            Return Nothing
        End Try
    End Get
End Property
Run Code Online (Sandbox Code Playgroud)

我可以有一个功能,它做同样的事情:

Public Function MaximumIndenture() As Integer
    Try
        'If the connection is closed, open it.'
        If _dbConnection.State = ConnectionState.Closed Then
            _dbConnection.Open()
        End If

        Dim dictFigureIndenture As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
        Using dbReader As IDataReader = ExecuteReader("SELECT PART_FIGURE, MAX(PART_INDENTURE) AS 'MAX_INDENT' FROM PARTS GROUP BY PART_FIGURE")
            While dbReader.Read()
                dictFigureIndenture.Add(dbReader("PART_FIGURE").ToString(), Convert.ToInt32(dbReader("MAX_INDENT").ToString()))
            End While
        End Using

        'If the connection is open, do what you need to and/or close it.'
        If _dbConnection.State = ConnectionState.Open Then
            _dbConnection.Close()
        End If

        Return dictFigureIndenture
    Catch ex As Exception
        'An exception was thrown.  Show it to the user so they can report it.'
        MessageBox.Show(ex.Message)

        'If the connection is open, do what you need to and/or close it.'
        If _dbConnection.State = ConnectionState.Open Then
            _dbConnection.Close()
        End If

        Return Nothing
    End Try
End Function
Run Code Online (Sandbox Code Playgroud)

他们基本上都做同样的事情,但我不确定哪个会在社区中被接受.最后我想写一些供人们使用的开源代码(大多数可能已经写过),但我绝对不希望有人认为我正在做的是最好的做法.任何人都可以给我一个外行描述,哪个更好用,为什么?对不起,如果这是一个重复的帖子给别人的,只是好奇.谢谢.

Guf*_*ffa 5

那应该是一种方法,而不是财产.

属性通常包含轻量级操作,例如获取私有变量的值,并且可能进行一些简单的计算或转换.从数据库中提取数据的东西绝对不符合一般预期的属性.