![]() |
Reflections on Property Let and GetAs you all may have discovered when doing the exercises, the Tools->Add Procedure->Property produces two blocks of code: a Property Let and a Property Get. If the property is of an object data type then instead of Property Let you get Property Set. The Set should look familiar because of its use in statements like
But where does "Let" come from? As programming languages evolve certain keywords become assumed. "Let" is one of those. Code that used to be written this way:
can now be written without the explicit Let:
Seeing these property constructs used in the user-programmer realm can help you in your god/dess/super programmer role. Property Lets are for simple data types like strings and numbers. Property Sets are for complex data types like other objects. Property Gets retrieve either kind of property. If you want a property to be read only then just have a Property Get. If the Tools->Add Procedure->Property dialog gives you a Property Let along with the Property Get, just delete the Property Let block. An example of a common VB object property that was created with just a Property Get is the hWnd property of a TextBox object. Pull up the VB Object Browser and run through the various properties of the TextBox class. Pay attention to the little blurb that describes the property. You'll see phrases like "read only" and "returns/sets". So, if I want to create, say, a lottery class to generate random numbers I'd need three properties (at least): lowest number, highest number and result. Lowest and Highest can be read/write (let-able and get-able), but Result should be read only (get-able) because having gone to the trouble of generating a random result I don't want all that hard work wiped out by an errant Result=2. The class module would like something like this:
But, wait! you ask - what is with those two Public variable declarations up top? And where are the Property Let and Get statements for Smallest and Largest?...
Instead of Smallest and Largest being explicitly Property procedures I declared them as Public variables. This is a quick and easy way to get read/write properties if you're not sure how to code Property Lets and the Gets don't involve calculations. It's not the recommended way, but it will get the job done in a pinch until you're more comfortable with coding Property procedures. VB doesn't just leave them as public variables. It's done something warm and fuzzy for us, as we can tell by looking at the type library under the OLE View tool (Start->Programs->Microsoft Visual Studio 6.0->Microsoft Visual Studio 6.0 Tools->OleView): // Generated .IDL file (by the OLE/COM Object Viewer) // // typelib filename: chance.dll [ uuid(A5EBFB20-7253-4235-8F14-0DAC93FD4779), version(1.0) ] library Chance { // TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046} importlib("stdole2.tlb"); // Forward declare all types defined in this typelib interface _Lottery; [ odl, uuid(A66A6FE6-6A38-43C7-A873-DCD8F57158D5), version(1.0), hidden, dual, nonextensible, oleautomation ] interface _Lottery : IDispatch { [id(0x40030000), propget] HRESULT Smallest([out, retval] short* Smallest); [id(0x40030000), propput] HRESULT Smallest([in] short Smallest); [id(0x40030001), propget] HRESULT Largest([out, retval] short* Largest); [id(0x40030001), propput] HRESULT Largest([in] short Largest); [id(0x60030001)] HRESULT RollEm(); [id(0x68030000), propget] HRESULT Result([out, retval] short* ); }; [ uuid(BB3DBBA0-9308-47DA-A2DD-B0F21BAE3947), version(1.0) ] coclass Lottery { [default] interface _Lottery; }; }; Yes, that's the cold and prickly C code from which VB is protecting you. Notice that Smallest and Largest have both propget and propput entries whereas Result has only a propget. Importantly, those public variables have been implicitly converted to property procedures for you. Pretty cool, eh? Alrak's Course Resources ©2002-2007 Karla Carter. All rights reserved. This material (including, but not limited to, Mini-lectures and Challenge Labs) may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder. For permission, contact Karla. |