VBScript property let, property get, property set

Source: Internet
Author: User

Class properties in VBScript are used to assign values to private variable and handle the process of data validation.

Property let: Which is used by the outside code to store a value in the private property variable. it is similar to a procedure in the sense that it does not return a value. A property let procedure must accept at least one argument. if the private variable you are using is an object then the process of assignment and data validation is handled by property set.Property set:Similar to property let but used for Object Based properties. By default, the property set procedure is public.

To retrieve the value of a private variable we will retrieve the value of a property.Property get: This is used by code outside of your class to read the value of a private property variable. it is similar to a function in the sense that it returns a value to the calling code -- this value is the private variable value.

The property get procedure does not accept any arguments. you can add an argument to it, but then you have to add an additional argument to the property's corresponding property let or property set procedure, because property let/set procedure must always have exactly one more argument than its corresponding property get procedure.

If the property get procedure returns an object then we can use the set Statement (but it works well without set also) to return the value.

Class ABC
'Private object
Private var_obj

Public property get username ()
Set username = var_obj
End Property
End Class

Read Only properties have only property get procedure

Write-only properties have only a property let or a property set procedure

Read-write properties have a property get procedure and either a property let or a property set procedure

Example 1 of property let, property get, property set

Below example, which shows a simple class that defines a private variable, m_var, And a two read-write properties, one_type and two_type, the latter of which is an object property.

Class computer

Private m_var
Private o_var

Public property let one_type (stringtype)
M_var = stringtype
End Property

Public property get one_type ()
One_type = m_varv end property

Public property set two_type (oobj)
Set o_var = oobj
End Property

Public property get two_type ()
Set two_type = o_var
End Property

End Class

Example 2 of property set

Here is the syntax for a property set procedure.

Class main_class
'Private fs_object object
Private var_obj
Public property set fspro (objfspro)
Set var_obj = objfspro
End Property
End Class

For example, here is what code that is using an object based on the above class might look like.

Dim objmain_class
Dim objfspro
Set objfspro = _ wscript. Createobject ("scripting. fs_object ")
Set objmain_class = new main_class
Set objmain_class.fspro = objfspro

Last line uses the set statement when it writes to the fspro property. this is required because the main_class class used a property set procedure for the fspro property. without the set statement at the beginning of the last line, VBScript wowould produce an error. when a property on a class is object based, it is typical to use a property set procedure. most programmers using this class wowould perform CT this.

Example 3 of property set

For example imagine we had a class that contained a private property named ob_var_conn that was expected to be An ADO connection object. This class definition, with the property set and property get statements might look like:

Class connect_class

'Create a private property to hold our connection object
Private ob_var_conn
Public property get connection ()
Set connection = ob_var_conn
End Property

Public property set connection (ob_var_connection)
'Assign the private property ob_var_conn to ob_var_connection
Set ob_var_conn = ob_var_connection
End Property
End Class

The end developer wocould use the property set statement in the following manner:

'Create an instance of connect_class
Dim ob_var_class, ob_var_record
Set ob_var_class = new connect_class
Set ob_var_connection = server. Createobject ('ADODB. connection ')
'Assign ob_var_connection to the connection Property
Set ob_var_class.connection = ob_var_connection

As with the property let statement, the property set statement has an optional argument list. This argument list must be identical to the corresponding property get's argument list.

Example 4 Of property let, property get, property set


Private recentpenpencil, recentcolor

Property get penpencil ()

Set penpencil = recentpenpencil

End Property

Property set penpencil (X)

Set recentpenpencil = x

End Property

Property get pencilcolor ()

Select case recentcolor

Case 1: pencilcolor = "orange"

Case 2: pencilcolor = "green"

Case else: pencilcolor = "yellow"

End select

End Property

Property let pencilcolor (X)

If X = "orange" then

Recentcolor = 1


If X = "green" then

Recentcolor = 2


Recentcolor = 0

End if

End if

End Property

End Class


Set one_penpencil = newPencilclass

Set two_penpencil = newPencilclass

One_pencil.pencilcolor = "orange"

Wscript. Echo one_pencil.pencilcolor

Set two_pencil.penpencil = one_pencel 'This Invokes property set

Wscript. Echo "1st time" & one_pencil.pencilcolor

Two_pencil.pencil.pencilcolor = "green"

Wscript. Echo "2nd time" & one_pencil.pencilcolor

Auto: http://www.editorial.co.in/software/vbscript_property_let_get_set.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.