我不太新到VB.Net和语法,但我不是专家。我正在重写以前用C#编写的VB中的东西,而当我这样做时,我遇到了一个“困境”。我可以有一个只读属性:VB.Net中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
我能有做同样的事情的函数:
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
他们基本上都做同样的事情,但我不知道这将是在社区更受欢迎。最后,我想写很多开源代码供大家使用(大部分可能已经写过),但我绝对不希望有人认为我所做的是最佳做法。任何人都可以给我一个外行的描述,哪个更好用,为什么?对不起,如果这是一个重复的帖子给别人的,只是好奇。谢谢。
btw。有免费的工具从C#转换为VB,看看这里http://www.developerfusion.com/tools/convert/csharp-to-vb/ – elsni 2010-09-28 12:12:15
@elsni - :)谢谢。但是我只是因为以前的C#版本不是最好的,所以我只能手工编写它。所以当我经历它并将其转换时,我正在改变事物。我一定会保存那个链接。 – XstreamINsanity 2010-09-28 12:15:49