Re: Using readonly properties in a class...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brad Pears

    Re: Using readonly properties in a class...

    Perfect. That makes more sense then calling a method to run the Stored Proc
    and return the value in the read only properties "Get" method because then
    it would be running that everytime someone wants to use that value.
    Instead, run it once at object instantiation, then set it once to the
    private variable, then the read only properties get method "gets" it from
    there when it is requested.. Makes a lot more sense...

    Thanks!

    "Jack Jackson" <jjackson@cinno vations.netwrot e in message
    news:dfivv35no1 rhdds3porvvl3mb 4ja0lk67u@4ax.c om...
    On Fri, 11 Apr 2008 15:58:36 -0400, "Brad Pears"
    <bradp@truenort hloghomes.comwr ote:
    >
    >>I have a class that has readonly properties in it. I am fairly new to OO
    >>design and development so please let me know if this doe snot make
    >>sense...
    >>
    >>The reasopn why I have these read only porperties is becasue these
    >>particular properties are not set programatically - they are as a result
    >>of
    >>a calualtion of two other properties as shown below...
    >>
    >>public readonly property PSFLiveFactored () as decimal
    >>Get
    > return PSFLiveFactored = PSFLive * 1.5
    >>end get
    >>end property
    >>
    >>I have one readonly property that is derived from an SQL Stored procedure
    >>(called when the object is instantiated) which selects a row from a view.
    >>This particular view includes some calculated rows - one of which I want a
    >>few of my read only properties to contain - as opposed to the way I am
    >>setting them above. Since I cannot (and do not want anyone to) set this
    >>property in code (hence a readonly property), how do I actaully set an
    >>initial value for a read only property?
    >>
    >>Would I have to call a private method in the "Get" area that would then
    >>retrieve the data from the DB and use that returned value to set the
    >>property?
    >>
    >>
    >>Thanks, Brad
    >
    Define a Private variable to hold the value from the stored procedure.
    Set that private variable after you execute the stored procedure
    during initialization. Then in the readonly Property's Get method use
    the private variable in its calculation.

  • Tom Dacon

    #2
    Re: Using readonly properties in a class...

    It's good practice to make object instantiation as "cheap" as you can. It's
    possible that some code might instantiate the object and never need to refer
    to that particular property, so the cost of running the stored procedure
    during initialization would be wasted.

    A technique that I mentioned in a different post allows you to run the
    stored procedure only the first time the property is retrieved.

    As follows (copied from the other post):

    Private _FieldValue As <some type= Nothing

    Public ReadOnly Property Something As <some type>
    Get
    If _FieldValue Is Nothing Then
    _FieldValue = GetFieldValueFr omSomewhere()
    End If
    Return _FieldValue
    End Get
    End Property

    Tom Dacon
    Dacon Software Consulting


    "Brad Pears" <bradp@truenort hloghomes.comwr ote in message
    news:%23TL9iLkn IHA.2352@TK2MSF TNGP05.phx.gbl. ..
    Perfect. That makes more sense then calling a method to run the Stored
    Proc and return the value in the read only properties "Get" method because
    then it would be running that everytime someone wants to use that value.
    Instead, run it once at object instantiation, then set it once to the
    private variable, then the read only properties get method "gets" it from
    there when it is requested.. Makes a lot more sense...
    >

    Comment

    Working...