Introduction to Class Programming

Property Get/Let

Every time you assign a new value to a property, Visual Basic checks whether there's an associated Property Let procedure and passes the new value to its associated Private variable. If the code can't validate this new value, it raises an error and throws the execution back to the caller. Otherwise, the execution proceeds by assigning the value to the Private variable.  And when the caller code requests the value of the property, Visual Basic executes the corresponding Property Get procedure, which simply returns the value of the Private variable. Try to trace your code by pressing F8,  and see what actually those property procedure do. 

NOTE
The type expected by the Property Let procedure must match the type of the value returned by the Property Get procedure.

You might be asking this question, "Why not just use a public variable to store property values?  In a sense, we are just using a sort of indirection. Why not use function to return a value or sub to assign a value" In some cases that may work fine; however, if you want to make sure that the value assigned to the property is valid, you have to write validation code when the value is assigned. And usually you this by creating a Sub or Function to validate the public variable or worst you end up coding a lot in the client (form) for this validation routine.  It end up, we did not benefit for the characteristic of OOP which is encapsulation, stated that "an object is the sole owner of its own data." And this is the benefit of using a property procedure.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor