|
-
May 26th, 2022, 05:58 AM
#1
Thread Starter
Hyperactive Member
Public Variables in a Class Module
Let's say I have a Class module that has 1 variable, "AA As Long". Let's further assume I need to set the value f this variable. If I need to screen the inputs (for example, set it to 0 if the incoming value is <0) then I can use a Property Let statement. But for argument's sake let's say I don't want/need to screen the value.
I know it is "not good programming practice" to declare "AA" as a Public variable in the Class module but to me it is a whole lot simpler and efficient to declare it as Public than having a Property Let and a Property Get statement to set/get the variable value.
What's wrong with a Public declaration in this case?
-
May 26th, 2022, 08:27 AM
#2
Re: Public Variables in a Class Module
With a Class Module, there can be several instances of the class running. What would be the point of declaring a variable as Public if you have to identify the particular instance that it belongs to anyway?
J.A. Coutts
-
May 26th, 2022, 08:32 AM
#3
Re: Public Variables in a Class Module
Honestly I don't think there's much, other than having get/let/set would make refactoring a bit easier later (somehting needs to change to read-only, for instance; or if you need to do somehting when one of the properties is set). I'm sure there's probably some fundamental difference under hte hood, but functionally they should be virtually identical.
Pros for having getters & setters - control, validation, generally accepted best practice
Pros for not having them - simplicity & speed (not of code but you typing).
-tg
-
May 26th, 2022, 08:37 AM
#4
Addicted Member
Re: Public Variables in a Class Module
 Originally Posted by MountainMan
What's wrong with a Public declaration in this case?
Nothing.
-
May 26th, 2022, 08:40 AM
#5
Re: Public Variables in a Class Module
 Originally Posted by MountainMan
I know it is "not good programming practice" to declare "AA" as a Public variable in the Class module but to me it is a whole lot simpler and efficient to declare it as Public than having a Property Let and a Property Get statement to set/get the variable value.
What's wrong with a Public declaration in this case?
Nothing's wrong with this per se. The compiler automagically generates actual Property Get/Let/Set for you behind the scenes and these are usually faster than manual ones so a win-win situation.
There is no such thing as "public data" in COM world like there is in C/C++ with struct/class declares so you cannot for instance get the address of MyObject.AA as this is not some public chunk of data but a Public Property Get call.
cheers,
</wqw>
-
May 26th, 2022, 12:05 PM
#6
Thread Starter
Hyperactive Member
Re: Public Variables in a Class Module
Couttsj,
Even if you have multiple instances of the class running, you have to refer to each Public variable with ClassName.VarName so there is no chance the variable names getting mixed up.
-
May 26th, 2022, 05:10 PM
#7
Re: Public Variables in a Class Module
 Originally Posted by MountainMan
Couttsj,
Even if you have multiple instances of the class running, you have to refer to each Public variable with ClassName.VarName so there is no chance the variable names getting mixed up.
That is exactly my point. Since you have to identify each target class, what is the point of declaring a variable Public?
J.A. Coutts
-
May 26th, 2022, 08:25 PM
#8
Thread Starter
Hyperactive Member
Re: Public Variables in a Class Module
You don't have to go through a procedure to set the variable value or to retrieve the value. I hope the trick shows the disassembled code for a call to a simple Public variable and then the same thing going through the procedure. I think the code for the direct reference to the variable takes about 1/3 the code. For a one-off called one time it doesn't matter but if you have a class module with 200 variables that you use over and over in a program, the overhead adds up.
-
May 26th, 2022, 08:38 PM
#9
Re: Public Variables in a Class Module
To be perfectly honest, I tend to default to using properties over public fields in classes whenever writing OO code. However, I would not hesitate to prefer public fields in VB6. I found that other languages have more concise syntax and modern IDEs like Visual Studio 2019 even help with auto-complete features. VB6 requires you to type out a whole lot of boilerplate property code. As such I will not frown on a VB6 programmer using simple public fields to avoid the teduim.
-
May 27th, 2022, 12:32 AM
#10
Thread Starter
Hyperactive Member
Re: Public Variables in a Class Module
wqweto,
I think I disagree with you. I put a utility in the CodeBank the other day that packs variables, properties and UDT's into/out of Byte arrays. For Properties in Class modules/forms I had to do exactly what you say in that I could not just copy a variable from a memory address nor could I put one back in.
However, I have tested a lot with Public variables in Class modules/Forms to see if I could use memory addresses like with variables in a standard module. I found that unlike properties, Public variables do have specific memory addresses and I can use RtlMovememory to access them just as if they were regular variables. I am pretty certain about this because I can write to thee memory address and read back form it and I get my original value back each time. That was not the case with properties. So I am convinced Public variables are treated just like any variables in a standard module, i.e., they have memory addresses and do not go through procedures.
-
May 27th, 2022, 03:32 AM
#11
Re: Public Variables in a Class Module
 Originally Posted by MountainMan
So I am convinced Public variables are treated just like any variables in a standard module, i.e., they have memory addresses and do not go through procedures.
If you actually believe this you might want to consult the actual code generated (which is actually real, being part of the reality). I'm just too lazy to prepare test projects anymore, disassemble and everything else just to disprove erroneous bombastic statements in these forums.
This is my understanding (of which I'm pretty sure): Public variables on class modules are transformed to private variables and accessor methods (so called property get/let/set in VB-land). You can get such backing private variables addresses, but no you cannot get a "public variable address" as in COM coclasses/interfaces there are no "public variables" per se and so there are no "public variables addresses" as a consequence.
In TwinBasic for instance (same language, different implementation) "public variables" might be implemented in a completely different fashion so you cannot poke some instance internals and claim to acquire "public variables addresses" out of thin air.
cheers,
</wqw>
-
May 27th, 2022, 06:12 AM
#12
Addicted Member
Re: Public Variables in a Class Module
You get the variable address, does it matter whether its scope is public or private?
The public scope is as a property, not as a variable.
When you get the variable address you get the address of the private variable that is holding the data.
(not that I tested anything, but it is simple logic).
-
May 27th, 2022, 03:09 PM
#13
Re: Public Variables in a Class Module
I only use property let /get if I need to protect an actually value or take multiple actions on it being set.
A public var in a class does indeed get auto wrapped in get/let’s by the compiler if you don’t do it.
The first time you ever tried to use copymemory varptr(myclass.myint)
You will scratch your head for quite a while trying to figure out *** is going on
Tags for this Thread
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
|