views:

259

answers:

3

Hello,

i`m working on the legacy project in VB6 with huge object all with public variables. I want to convert these public variables to private variable/property combinations. Is there some tools with which i can do these conversion?(optimally all variables in class at once)

Thanks

+1  A: 

I don't know of any refactoring tool for VB6, but I'd approach the problem by writing a script to scan the source file, search for lines matching "Public Dim ..." and replacing those lines with the appropriate "Private Dim ..." and property accessors.

Vinay Sajip
Good answer - although the Dim is optional, it is legal to just write `Public X` or `Public X As sometype`.
MarkJ
+3  A: 

Don’t do the conversion, it’s not needed. The VB compiler does this automatically for all exposed classes (i.e. all classes that are exported in COM DLLs) and it’s not required for all other fields since these are used internally only and there’s no difference between a field and a property for the user.

VB6 is the only language to do this right, in not allowing public fields at all, and implicitly converting them.

To recap: there’s nothing wrong with public variables in VB6 since the usual disadvantages of public variables don’t apply to them. In particular, they don’t break encapsulation.

Konrad Rudolph
yes i know this, but i must also to set in these properties one special variable(this is not very good thing i knoooow, but i must do this)
Cicik
+10! One interesting niggle proves the compiler does this. Suppose you have a routine Convert(ByRef x as Double) that modifies its argument x. If you call it with a public variable, like Convert(myobj.Prop) - the changed value *won't* be stored into the public variable. This is because it is wrapped as a property just as Konrad described. I believe in fact this also happens for private classes as well as public ones. So as Konrad states, using public fields doesn't break encapsulation because they can be replaced with a full property at any time **without** altering behaviour for the clients.
MarkJ
It still insists on breaking binary compatibility if you migrate a public variable to a public property though :(
rpetrich
+2  A: 

MZ-Tools is a free add-in that has a feature that allows converting a public variable to a property.

C-Pound Guru