Properties, Methods, and Events

 < Day Day Up > 



Associated with every object is a collection of properties, methods, and events, which are used to communicate information between the object and the routine that created the object.

Public vs. Private Properties, Methods, and Events

The individual parts of a class can be labeled as either Public or Private. Anything marked as Public is visible to anyone using the class, whereas anything marked as Private can be accessed only from the code within the class.

Tip 

Don’t Rely on Defaults
Always explicitly mark everything within a class as either Public or Private, so you never have to worry about whether something defaulted to Public or Private.

Properties

Within a class, you’ll find a number of different things. You can define class-level variables, which can be accessed from anywhere in the class. If a class-level variable is marked as Public, it becomes available to anyone using the class and is then known as a Property of the class.

In addition to public class-level variables, properties might also have code associated with them. Each property where you want to use code is organized into two routines, which return the value to the calling program or change the value in the class. The Get routine returns a value to the caller, whereas the Let or the Set routine allows the caller to assign a value to the property.

Remember that you can define a property with only a Get routine or a Let or Set routine. If you use only a Get routine, the property becomes read-only and it’s value can’t be changed by the program using the object. Likewise, if you include only a Let or a Set routine, the property becomes write-only, and its value can’t be viewed by the calling program.

Methods

Along with class-level variables and property routines, a class can also contain a series of regular subroutines and functions. If a function or a subroutine is marked as Public, it’s known as a method. Methods can be invoked from code residing inside or outside of the class.

Events

Events are subroutines that exist outside the code associated with the class that can be called from statements within the class. Events provide a way for a class to interrupt the program that created an instance of the object from the class, thereby allowing the program to perform its own processing in response to a situation encountered by the object. Remember that the code associated with an event actually resides outside the class. The only information stored within the class is the event’s definition, including the parameters that will be passed to the external program.

Note 

Classes containing events require the WithEvents keyword to be placed in the object’s declaration. If the declaration doesn’t include this keyword, any events that occur will be ignored by the external program.

Together these public properties, methods, and events of a class provide the interface to the object. The interface isolates the code inside the class from the code that uses the objects created from this class. This isolation is highly desirable for several reasons. First, it allows you to test the class independently of the rest of the application. Once you are satisfied that the class is stable, you can treat it as a black box that simply works. Second, it makes it easier for multiple people to work on a single application. One person can work on the class, while others work on code that use the class. Third, it’s possible to modify the code inside the class without necessarily impacting applications that use the class. This way you can implement new algorithms or add new features to the class without changing the programs that use the class.



 < Day Day Up > 



Microsoft Excel 2003 Programming Inside Out
Microsoft Office Excel 2003 Programming Inside Out (Inside Out (Microsoft))
ISBN: 0735619859
EAN: 2147483647
Year: 2006
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net