You can expose properties in two ways:
You can also mark a property as the default value for a script component.
To expose a property as a simple value
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="name"/> <property name="tag" internalName="tagVar"/> </public> <script language="VBScript"> <![CDATA[ Dim name name = "script component" ' Initializes the value of name property. Dim tagVar tagVar = "10" ' Initializes the value of tag property. ]]> </script>
Exposing a property using functions is similar to exposing a method.
To expose a property using functions
The names of the procedures must match the internal names you specified in the <property> element. If you did not specify an internalName attribute, the names of the functions must be the name of the function with the get_ prefix for a read function, and with a put_ prefix for the write function.
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>
</public>
<script language="VBScript">
<![CDATA[
Dim sname ' Read-write sname property (no functions).
Dim gDOB ' Global variable used to store date of birth.
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
]]>
</script>
You can specify a default property for a script component so the host application can get or set the property's value without explicitly naming the property. For example, if you have exposed a property called sname and marked it as the default, you can work with it in either of these ways in Visual Basic:
Set component = CreateObject("Component.MyComponent")
n = component.sname ' Gets property explicitly.
n = component ' Gets value of sname as default.
To specify a default property, include an attribute assigning a special dispatch identifier (a dispid) to the method. For more information about dispids, see Exposing Events.
To specify a default property
<property name="sname" dispid="0"/>
Note This technique can be used to assign either a default property or a default method, but not both. There can be only one element in the script component with the dispid of zero.
Exposing Events | Exposing Methods | Script Component File Contents | Script Component Files and XML Conformance