Advice on object property

Last post 07-02-2009 3:36 PM by scott@elbandit.co.uk. 9 replies.

Sort Posts:

  • Advice on object property

    06-30-2009, 1:55 PM
    • Contributor
      5,788 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,215

     I want to preface that the easiest solution to this question is just to create another property with a different name, but I do not want to do that.

    I have the following properties:

            Public ReadOnly Property TotalApples() As Integer 
                Get
                    Return (mRedApples + mGreenApples)
                End Get
            End Property
    
            Public Property RedApples() As Integer 
                Get
                    Return mRedApples
                End Get
                Set(ByVal value As Integer)
                    mRedApples = value
                End Set
            End Property
    
            Public Property GreenApples() As Integer 
                Get
                    Return mGreenApples
                End Get
                Set(ByVal value As Integer)
                    mGreenApples = value
                End Set
            End Property


    This works perfectly for what I need.  However, sometimes I need to set the 'TotalApples' property manually and do not want to use the default calculation.  The easiest way may be to change the exsisting property to be called 'TotalApplesCalculated' and then make 'TotalApples' a property that has the traditional Get/Set accessors.

    I really just wanted to put this basic question out there to ask if there is a good way to use the one 'TotalApples' property, or if there is a better way to do this without using a second property.

    Thanks!  Laughing

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Advice on object property

    06-30-2009, 2:49 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

    If you want to set the totalapples property manually, you have to use the

    Set

    {

    }

     accessor inside th property and remove the "Readonly" access modifie from the property

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: Advice on object property

    06-30-2009, 2:58 PM
    • Contributor
      5,788 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,215

    No the question was a little more involved than that, please read it again.  You can not use a 'Set' accessor on the 'TotalApples' because it returns the Sum of the other (2) properties.  Having a set accessor makes no sense, because there is nothing to set.  It is a calculated read-only value that is returned.  If I was to set a value, it would never be returned because it is not part of the formula.  I thought about adding a Set, and checking if the value set has any value and if so return the raw value otherwise the calculated value, but '0' could be a valid value and that check will not work.

    Anyone else have any ideas (please read the original post carefully, thank you!) 

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Advice on object property

    07-02-2009, 3:54 AM
    Answer

     It might help if you gave some context as to why you would want to set the total apples count. Here is a technique you could use:

    Private mRedApples As Integer
        Private mGreenApples As Integer
        Private mTotalApples As Nullable(Of Integer)
    
        Public Property TotalApples() As Nullable(Of Integer)
            Get
                If mTotalApples Is Nothing Then
                    Return TotalCalculatedApples()
                Else
                    Return mTotalApples
                End If
            End Get
            Set(ByVal value As Nullable(Of Integer))
                mTotalApples = value
            End Set
        End Property
    
        Public Function TotalCalculatedApples() As Integer
            Return (mRedApples + mGreenApples)
        End Function
    
        Public Property RedApples() As Integer
            Get
                Return mRedApples
            End Get
            Set(ByVal value As Integer)
                mRedApples = value
            End Set
        End Property
    
        Public Property GreenApples() As Integer
            Get
                Return mGreenApples
            End Get
            Set(ByVal value As Integer)
                mGreenApples = value
            End Set
        End Property

    I hope thats helpful to you.
    Cheers
    Scott

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: Advice on object property

    07-02-2009, 8:56 AM
    • Contributor
      5,788 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,215

    The purpose for setting the value rather than just using the calculated value was how the data came back from the database.  I have most of my stored procedures bring back the red apples and green apples separetly, and then are populated on the object.  In this case the calculated property works perfectly.

    However, there are a few instances where the total is calculated in the stored procedure and needs to just be set on a property without it being calculated.  I like your example and did something similar while waiting for a response from this thread.

    (2) questions for you:

    1.  Does this info I provided change your suggestion?

    2.  Why make TotalCalculatedApples a function rather than keep as a property (which is fine as a funtion but just interested in your thoughts)?

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Advice on object property

    07-02-2009, 10:16 AM

    atconway:
    However, there are a few instances where the total is calculated in the stored procedure and needs to just be set on a property without it being calculated.  I like your example and did something similar while waiting for a response from this thread.
     


    Does this mean that when you are setting the calculated value from the SP you are not setting the RedApples and GreenApples properties?

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: Advice on object property

    07-02-2009, 10:58 AM
    • Contributor
      5,788 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,215

     Correct.  I could go back and do that, but the SP is huge and I was hoping not to have to do that.  The values end up getting loaded into a list of objects.  There end up being 108 pre-calculated totals returned from the SP.

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Advice on object property

    07-02-2009, 11:07 AM

    Hmmm to help me understand better lets name the class with the apples properties on it, what is it supposed to represent? It may be that you have different requirements and need a seperate reporting object rather then using one for two different jobs?

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: Advice on object property

    07-02-2009, 1:43 PM
    • Contributor
      5,788 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,215

     Well at this point it is ahed to answer the questions because I am sure you figured out I am not writing an app about apples, but they made a perfect and easy analogy to my business specific process.  I don't have a good way to draw an anaogy to my class to the apples example, but your comments keep my thought process on track of when to think about creating a new object.  I do however think in this case that the one property still does belong to the same class.  I was just looking to see if there was some ingenious way to solve my issue I had not thought of.

    One quick last important question.  I noted earlier that I could not test to see if TotalRedApples or TotalGreenApples was zero (0) to determine if I should return the calculated value or the set value because a value for 0 for either red or green apples is valid and also the default value of the property, but could also be the amount that comes back from the db..  Your example does this, but with 1 variation.  The 'nullable' type.  Is that what would allow me to make that comparison?

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Advice on object property

    07-02-2009, 3:36 PM
    Answer

    Yes the nullable keyword enables value types to have a null value, so you can determine if a value has been assigned or not. For more info check out the msdn doc http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx.

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
Page 1 of 1 (10 items)