views:

25

answers:

3

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.

+4  A: 

That should definitely be a method, not a property.

A property generally contains a light weight operation, like getting the value of a private variable and perhaps do some simple calculation or conversion. Something that pulls data from a database does definitely not match what's generally expected of a property.

Guffa
That's what I was thinking. But I was also thinking, "Well, they allow you to put code in there, not just a simple return, so why not?" And I know I can even put breakpoints in there. I didn't know if it was for aesthetics reasons either. Thanks for the response.
XstreamINsanity
Agree with above, additionally i'd say that a function could have side effects (do something to change state or data elsewhere) but a property never should.
MikeG
a readonly property is every bit as powerful as a function in terms of langauge, debuggability etc.. Your decision is definatelt about aesthetics. i.e. When another programmer uses your property or function what do they expect to happen...
MikeG
At first when I had this as a property I was having it pull just the maximum indenture of the selected figure in the TreeView. But then, I figured get them all at the beginning of the application since those figures will never change and I'll just hold onto them. I'm starting to think I should to back and just get it on every selection. It would still look the same, but might make more sense aesthetically. However, this is in a dataFactory class, not in a class for the "figure" itself.
XstreamINsanity
A: 

I can't promise you this is the best practice but I would say go with what feels more natural to you and makes more sense in the real world. I would say MaximumIndenture would make a better property than a function because it seems to me it is more of property of the object or a noun that belongs to it. I would use a function to do more of an opperation or verb that has some result. Those are my 2 cents. Hope it helps!

Mike
:) That is exactly my dilemna. Yeah, I could make the function "GetMaximumIndenture" and I don't lose anything by doing so, but aesthetics wise, if it's a part of an object, MaximumIndenture feels and sounds better.
XstreamINsanity
+1  A: 

Use a method, rather than a property, if the operations is orders of magnitude slower than a field access would be. In particular operations that access the network, or a file system, should likely be methods.

Your code accesses a database: so it should be a method.

From the .NET Framework Design Guidelines 2nd Edition page 135

MarkJ
Thanks. I was just looking for the DVD for that since someone on a site said it was free for download from the publisher's site, but I don't see it there anymore. I'm not sure I agree with one thing I saw in the book though, lol. I'm not fond of placing an opening brace at the end of the line. Doesn't look right to me. :)
XstreamINsanity
@Xstream Main contents of the DVD is a video presentation on Designing class libraries. It is supposed to be [available](http://www.researchchannel.org/index.asp) on the Research Channel but sadly the Research Channel appears to be down. <Humour> If you can't decide where to put your braces, just switch to VB. Spend less time in pointless arguments, and you avoid RSI in your right little finger too! :) </Humour>
MarkJ
lol. Yeah, everyone tells me to switch to VB, but probably because of my schooling, C# just feels so natural. And Braces seem more organized. But that's just my opinion. I may pick up that book though, it seems pretty nice.
XstreamINsanity