Results 1 to 2 of 2

Thread: Creating a Property

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    karachi
    Posts
    90
    Hello,
    Can any one tell me how to create
    and what it the beneift of creating
    property with easy example

    Bye

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You create properties by writing property procedurs. These a two different procedures for one property.
    The first procedure is for assigning values. It's called a Property Let procedure (or a Property Set procedure if the property actually is an object like Font or Picture).
    The second procedure is for retrieving values and is called Property Get procedure.
    Here's a simple example:
    Code:
    'local variable to store the property value
    Private m_sFirstName As String
    
    Public Property Let FirstName(sNewValue As String)
        'store the new value in the local variable
        m_sFirstName = sNewValue
    End Property
    
    Public Property Get FirstName() As String
        'return the stored value
        FirstName = m_sFirstName
    End Property
    As you can see the Property Let procedure is like a sub that takes an argument (the new value) while the Property Get procedure is like a function that returns the stored value.

    You can always argue that this could just as well been a public variable that stored the first name.
    Well consider that you might want to check if the value is valid before assigning it.
    Maybe you just want to convert the FirstName into proper case. You can't do that with a public variable because you can't tell when it's assign a new value. But with a property procedure you can.
    Code:
    Public Property Let FirstName(sNewValue As String)
        'Check the new value so it's not an empty string
        If Len(sNewValue) > 0 Then
            'convert the value to proper case and then
            'store it in the local variable
            m_sFirstName = StrConv(sNewValue, vbProperCase)
        End If
    End Property
    You can also make a property read only simply by only writing a Property Get procedure and leave the Property Let procedure out.
    This can't be done by using a public variable.

    Best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width