Partager via


<property> Element

 

Declares a property.

Syntax

<property name="propertyName" [internalName="propertyVariable"] />

–or–
<property name="propertyName">
   <get [internalName="getFunctionName"] />
   <put [internalName="putFunctionName"] />
</property>

Values

  • propertyName
    The name of the property to expose. Unless you specify an internalName attribute, this name must match the name of a global variable that will be used to hold the property's value.

  • propertyVariable
    (Optional) The name of the global variable in the scriptlet file's <script> element that will hold the value for propertyName. If you do not include the internalName attribute, the property value is maintained in a variable named the same as propertyName. Specifying the internalName attribute allows you to use different names for the property and its corresponding global variable.

  • getFunctionName
    (Optional) The name of a procedure that is used to read the value of the property. If you include <get> element but no <put> element, the property will be read-only. If you include a <get> element but do not specify an internalName attribute, the method used to read the property value must have the name of the property plus the get_ prefix (for example, get_lastname).

  • putFunctionName
    (Optional) The name of a procedure that is used to write the value of the property. If you include a <put> element but no <get> element, the property will be write-only. If you include a <put> element but do not specify an internalName attribute, the method used to read the property value must have the name of the property plus the put_ prefix (for example, put_lastname).

    Tip

    In XML, you can implement elements that have no content (such as the <property> element) by closing the element with />.

Remarks

Properties can be exposed as simple values. In that case, the property is treated as a global variable inside the script component file.

You can also implement properties as procedures (functions or subroutines), which allows you to calculate a property value and to control whether a property is read-only, read-write, or write-only. In this technique, properties are implemented as a procedure (function or subroutine) in a separate <script> element. The <property> element maps the name of the property to the procedures used to implement it. The names of the procedures must match the internal names you specified in the <property> element.

When putFunctionName is called, it is passed one argument that contains the value to which to set the property.

In addition to the standard syntax shown above, you can use a shorthand notation to specify information what would otherwise be added using child tags. For example, if you want to declare a property with a "get" and "put" accessor of the same name as the property, you can use the following syntax:

<property name="myproperty" get put/>

which is functionally equivalent to:

<property name="myproperty"> 
   <get/>
   <put/>
</property>

If you wanted to explicitly name those accessors differently from the default, you can use the following syntax:

<property name="myproperty" get="testget" put="testput"/>

To specify a default property, include the attribute dispid="0" in the <property> element. For details, see Exposing Properties.

The following script component fragment shows the definition for four properties (sname, age, dateOfBirth, and mstatus). The sname property is a simple value. The age property is read-only, and is implemented with the function readAge. The dateOfBirth property is read-write, and is implemented with the functions readDOB and writeDOB. Finally, the mstatus property is implemented with the default functions get_mstatus and put_mstatus.

Note

A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.

<public>
   <property name="sname"/>
   <property name="age">
      <get internalName="readAge"/>
   </property>
   <property name="dateOfBirth">
      <get internalName="readDOB"/>
      <put internalName="writeDOB"/>
   </property>
   <property name="mstatus">
      <get/>
      <put/>
   </property>
</public>

<script language="VBScript">
<![CDATA[
Dim sname   ' Read-write sname property (no functions).
Dim gDOB   ' Global variable used to store date of birth.
Dim gMStatus   ' global variable used to store marital status.

Function readDOB()
   ' Gets value of dateOfBirth property.
   readDOB = gDOB
End Function

Function writeDOB(newDOB)
   ' Sets value of dateOfBirth property.
   If isDate(gDOB) Then
      ' Error checking.
      gDOB = newDOB
   End If
End Function

Function readAge()
   ' Calculates read-only age property.
   If isDate(gDOB) Then
      dobM = DatePart("m", gDOB)
      dobD = DatePart("d", gDOB)
      dobY = DatePart("yyyy", gDOB)
      todayY = DatePart("yyyy", Date)
      age = todayY - dobY

      ' Adjust if birthday has not yet occurred this year.
      bday = DateValue(dobM & "/" & dobD & "/" & todayY)
      If DateDiff("d", bday, DateValue(Date)) < 0 Then
         age = age - 1
      End If
      readAge = age
   End If
End Function

Function get_mstatus()
   ' Reads value of mstatus property.
   get_mstatus = gMStatus
End Function

Function put_mstatus(s)
   ' Writes value of mstatus property.
   If s = "M" Or s = "S" Then
      gMStatus = s
   Else
      gMStatus = "?"
   End If
End Function
]]>
</script>

See Also

<event> Element
<method> Element
Exposing Events
Exposing Methods
Exposing Properties