When it comes to Windows Script Components, it’s important to have a firm grasp of VBScript Classes, including when to use Public vs. Private properties.

Class Theory

When it comes to Windows Script Components, it’s important to have a firm grasp of VBScript Classes, including when to use Public vs. Private properties.

When I started this column almost two years ago, I made several promises. One promise was to follow a Challenge/Solution format. With each column, I try to give you something you can use immediately, while at the same time teaching you the theory necessary to move beyond that task and into writing more advanced solutions for different tasks.

Another promise was that I would, on occasion, cover topics “guaranteed to make even the most GUI-dependent server jockey feel like a born-again code monkey.” Well, in case you hadn’t guessed by now, VBScript Classes is one such topic! Sure, I’ve dealt with classes before, by way of built-in and third-party components, but this is a whole new ball game.

I’m reminding you of all this now because, this month, I won’t be looking at an administrative problem/solution. Instead, I’m going to cover mostly theory, in order to give you a firm understanding of classes before I move into our next topic-Windows Script Components. You see, regardless of how object-oriented and just generally cool classes are, you still must manually cut and paste them into your scripts. Windows Script Components, on the other hand, are treated just like regular components (e.g. the FileSystemObject, Wscript.Shell, and countless third-party components I’ve looked at.) In fact, the scripts that use them can’t tell the difference. But I’m getting ahead of myself.

Procedural Properties
Rule No.2 for classes (which I discussed last month) was that Private properties could still be set up to allow their values to be set externally (i.e. from the script that uses the class). This is done via Procedural Properties. Procedural Properties are Methods that are treated like Properties. There are three types: Let, Get and Set. Let’s compare Procedural Properties to standard Properties:

' The following classes create the same property
Class Standard
   Public MyProperty
End Class

Class ProcProp
   Private strMyPropValue
   Public Property Let MyProperty(strValue)
     strMyPropValue=strValue
   End Property
   Public Property Get
MyProperty()
     MyProperty=strMyPropValue
   End Property
End Class

Each class creates a property called MyProperty. The Standard class does this by simply creating a Public Property. The ProcProp class does this by creating two Procedural Properties. The Public Property Let statement is used to set the value of the property. The Public Property Get statement is used to query this value. Sure looks like an awful lot of typing to get the same result. Why would you want to subject yourself to that? Glad you asked!

Using Procedural Properties gives you some interesting advantages over standard properties:

Read-Only/Write-Only—Suppose you’ve written a class to perform User/Group management in the domain. One piece of information you might want to know is a total number of users. However, you don’t want anyone to be able to directly change this value, since it’s derived by counting each user account. (If someone did change it, you might experience, umm, “unpredictable” results!) By placing this value into a Private Property, you can use a Public Property Get to query it. To keep it Read-Only, you simply don’t create a corresponding Public Property Let.

By the same token, you can make a property Write-Only by creating a Property Let but no Property Get.

Hungarian NotationBy now, I’m sure you know that I always preface a variable name with an indication of its type (str for String, i for Integer, obj for Object, and so on.). What you may not know is that this is based on a standard naming convention called Hungarian Notation. Following this convention helps keep scripts organized. (No... I don’t follow it letter-for-letter, even though I should!) Unfortunately, Hungarian Notation isn’t standard in classes. You’ll never see a Public Property called strMyProperty or bMySwitch. It’ll just be MyClass.MyProperty or MyClass. MySwitch.

You’ll notice in the ProcProp class above, I’m really using a Private Property called strMyPropValue within the class. This allows me to code my class using Hungarian Notation, but still makes the property available to the “Public” using a more descriptive property name. For this reason, many scripters will never use Public to declare a property, but instead will use Private properties and create Procedural Properties for each one that needs external access (either Read-Only, Write-Only or both). I leave it to you to decide which method works best in your organization.

Default PropertiesYou’re allowed to create one very special property in your class called the Default property. I’ll use the same User/Group example that I used when talking about Read-Only properties and assume that you want the Default Property to hold the total number of users in the domain:

' Here's the class...
Class DomainUsers
   Public Default Property Get UserCount
       UserCount=50 ' This is arbitrary...
         '
You would actually use this space to
         ' execute code to go out and count the
         ' users.
   End Property
End Class

Because we don’t have a Property Let, this will be a Read-Only property. By making UserCount our Default property, this code:

' GetDirectValue
Set clsMyClass=New DomainUsers
iTotalUsers=clsMyClass.UserCount

will return the same value (50) as this code...

' GetDefaultValue
Set clsMyClass=New DomainUsers
iTotalUsers=clsMyClass

This gives your class a default property that can come in handy, but it’s by no means a requirement.

Property SetProperty Set’s used when you need to use a procedural property to pass an object for use by the class, rather than just a standard variable. In the past, we’ve used several objects (like the FileSystemObject) that pass objects back. Property Set allows you to pass objects to your class. Everything else works the same.

Mark My Last Words
One final word about Public and Private. When declaring Methods (e.g. Public Sub MyMethod, Private Function MyFunction, etc.), you’re not required to specify whether they are Public or Private. If you leave it out and just declare your methods with Sub MyMethod, it’s assumed to be Public. This can get you into trouble quick, so I recommend always specifying this to avoid confusion.

Congratulations! You’ve successfully completed the prerequisite course: “Theory of VBScript Classes”! Next month we’re going to take this knowledge and create Windows Script Components for administrative tasks. It’s gonna be a blast!

About the Author

Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to [email protected].

Featured

comments powered by Disqus

Subscribe on YouTube