Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public variable vs. Property statements...?

Status
Not open for further replies.

jitter

Technical User
Sep 2, 2001
351
0
0
US
For a read/write property in a class module that does not do anything specific during either operation:

Is it bad to just use a public variable instead of a property get and a property let statement?


Public MyProperty As Variant

vs...

Private pvntMyProperty As Variant

Public Property Get MyProperty() As Variant
MyProperty = pvntMyProperty
End Property

Public Property Let MyProperty(ByVal vNewValue As Variant)
pvntMyProperty = vNewValue
End Property


I recently read that one advantage of Public variables as properties are they are faster. What are the disadvantages of using them in the above situation?

Your input would be greatly appreciated!

:)
 
The main advantage of using Property Let/Get is that it allows you to verify the data or trapping the assignment. For example

Public Property Get MyProperty() As Variant
MyProperty = pvntMyProperty
End Property

Public Property Let MyProperty(ByVal vNewValue As Variant)
If vNewValue = "BadValue" Then Err.Raise 5, , "Bad Value Was Supplied"
pvntMyProperty = vNewValue
End Property

This is really helpful with a Variant Type as it allows you to control the property assignment for different types. You could check the value to see if it was an Long and assign one value and assign another if it was a string.

You cannot control a bad value with a public property form with in the class. I hope this helps. If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
Is it bad to just use a public variable instead of a property get and a property let statement?

Bad? No. Be prepared to create Property Let/Get accessors when you need them, though: don't be tempted to handle validation or other processing outside the class to save work - you'll just create it in the long run.

Which is faster? Who cares? There may be a microsecond or so difference, but you're unlikely ever to notice. Try it. Never mind, I just did. Property Get/Let was about 2.8 times slower (on an extremely simple test). But mymachine still managed about 1,000,000 Get/Let pairs per second. Which I'd say was about fast enough...
 
Thanks to both of you for the input.

In this case I guess I was more curious than anything. Our policy strictly prohibits us from using Public variables as properties and I wanted to know if there would be a specific reason for not using them.

In almost all of the cases we never do any validation on the property. And they are all almost always both read and write.

Call me lazy but I just don't see why having 30 or more properties in a class and using property get and set statements. Yeah we just copy and paste and each one specifies the variable type but I don't know. Just seems like a waste.

Why write


Private pstrFirstName as string
Private pstrLastName as string

Public Property Get FirstName() As String
FirstName = pstrFirstName
End Property

Public Property Let FirstName(vValue as String)
pstrFirstName = vValue
End Property

Public Property Get LastName() As String
LastName= pstrLastName
End Property

Public Property Let LastName(vValue as String)
pstrLastName = vValue
End Property


When I can do the same thing with:


Public FirstName As String
Public LastName As String


I guess they have their reasons though.

To me though after reading your input. It is faster and less code so why not use it if you don't need any of the advantages of Property statements.

Thanks again....
 
Jitter,

The advantage of always using them is consistency. Consistent code will be easier for you colleagues to read. It doesn't take long to write but it might save a lot of time in debugging.

Craig
 
Hi All,

The reason for Property procedures are for enabling encapsulation of code.

Use property procedures when:

The property is read-only, or cannot be changed once it has been set.

The property has a defined set of values or needs to be validated.

Values outside a certain range — for example, negative numbers — are valid for the property's data type, but cause program errors if the property is allowed to assume such values.

Setting the property causes some perceptible change in the object's state, as for example a Visible property.

Setting the property causes changes to other internal variables or to the values of other properties.



Use public variables for read-write properties where:

The property is of a self-validating type. For example, an error or automatic data conversion will occur if a value other than True or False is assigned to a Boolean variable.

Any value in the range supported by the data type is valid. This will be true of many properties of type Single or Double.

The property is a String data type, and there's no constraint on the size or value of the string.


MSDN states:-
Don't implement a property as a public variable just to avoid the overhead of a function call. Behind the scenes, Visual Basic will implement the public variables in your class modules as pairs of property procedures anyway, because this is required by the type library.


Codefish
 
Thanks all of you once again.

Thanks for your input codefish. That makes sense.

I think our company if more along the lines like Craig says. They never really gave me an answer that made sense but I am sure that is all just comes down to consistency and honestly nobody really cares about different ways.

Thankfully you all have been able to at least give me valid understanding of the difference between the two and when and why to use each.

Thanks....:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top