I'm not too new to VB.Net and the syntax, but I'm not an expert. I'm working on rewriting something in VB that was previously in C# and while I was doing so, I came across a "dilemna." I can have a ReadOnly Property:
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
And I can have a function that does the same thing:
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
They both essentially do the same thing, but I'm not sure which would be more accepted in the community. Eventually I'd like to write a lot of open source code for people to use (most has probably already been written) but I definitely don't want someone to think what I'm doing is the best practice. Can anyone give me a laymen description as to which is better to use and why? Sorry if this is a duplicate post to someone else's, just curious. Thanks.