|
-
Nov 20th, 2000, 07:40 AM
#1
Thread Starter
Hyperactive Member
Hi,
After reading about classes, this isn't clear to me. When should I use private variables 'm_something' (for property Get & Let) and when should I use Public (without Get & Let)?
I know abuout Let allowing me data validation. So for things like Name of object (or any String variable) could declare variable Public and forget about Let & Get or is Let & Get better in any way?
As always Thank you.
-
Nov 20th, 2000, 08:08 AM
#2
Fanatic Member
If you are using VB after version 4, there is no two ways about it. Use Let and Get. If you use a public variable in a class, VB implements hidden Let and Get procedures automatically! This can cause problems using your class because the variable cannot be passed by reference because you pass the result of the hidden Get procedure and not the memory address of the variable itself.
Use Let and Get. Every Time.
Cheers,
P.
Not nearly so tired now...
Haven't been around much so be gentle...
-
Nov 20th, 2000, 08:11 AM
#3
Addicted Member
You are absolutely right - you could just use a public variable.
I prefer to use the property procedures though, because if I need to change anything later on, like make a property read-only, or take any action when a property changes, then I can do so without changing the way the property was implemented.
Also, when using property procedures, you can type in a description for the properties, which shows up in the object browser. This is very handy if you are sharing your coding with others, since it makes it easier for them to use my classes.
And lastly, I like to stick to a standard. When creating ActiveX controls, I have to use property procedures, otherwise I cannot call the PropertyChanged statement when a property changes. And because I do it in AxtiveX controls, I do it everywhere.
But that is just my preference. You can use public variables as properties.
Shrog
-
Nov 20th, 2000, 08:11 AM
#4
Hyperactive Member
some input to your quesiton
Prop Get/Let gives *structure* to your code. It allows you to define and make clear just by looking at the declare statement what your internal variable is supposed to be. It also provides a way to keep members of your class strictly *read-only*, if you eliminate the Prop Let routine, or write-only if you eliminate the Prop Get.
I'll bet money that a public variable, especially with the public sub or function needed for computation, takes up a lot more resources than Prop Get/Let.
-
Nov 20th, 2000, 08:27 AM
#5
Addicted Member
Ah yes, that too...
I don't know about it using more resources, but what Paulw says is absolutely true, and the problems that arise are not worth the lazyness of just using public properties.
I'd actually forgotten about that problem. The reason I've forgotten about it is because I never use public variables as properties, and that's a good enough reason for me.
Shrog
-
Nov 20th, 2000, 09:54 AM
#6
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|