What is the difference between Public Property, Friend and Public Variable in VB6
Solution 1
The scope qualifiers Public
and Friend
determine whether clients in different projects can see the item.
-
Public
items will be accessible to client code in other projects1 and code in the same project. -
Friend
items are accessible only to code in the same project, not to code in other projects. -
Private
items are accessible only to code in the same class.
Properties are different from public variables, because with properties you can execute your own code when the client gets or sets the value2. EDIT following Deanna's comment: Also note that variables can be passed ByRef to a function and changes will work as expected. This is NOT the case for properties.
NB C# may be more modern, but IMHO the VB6 treatment of properties and public variables is significantly better than the .Net treatment.
- In VB6 you can change a public variable into a property without breaking the clients. You don't even have to recompile them. Not true in .Net.
- In VB6 public variables can be used with data binding. Not true in .Net.
- In VB6 public variables could be used with interfaces. Not true in .Net.
IMHO Microsoft made a real design mistake in creating these differences between properties and public fields in .Net. Not convinced? After the first releases of .Net, the C# and VB compilers were modified to support automatically implemented properties. These allow you to create properties in just one line of code, so that it's later possible to add logic on get/set without causing problems. IMHO this proves that public variables should have been made indistinguishable from properties.
1 Assuming your project type actually allows your classes to be used by other projects (i.e. ActiveX DLL, OCX, or ActiveX exe).
2 In the Property Get
, Property Let
and Property Set
procedures.
Solution 2
- Public means that it is accessible by any other classes that references your project/dll.
- Friend means that it is accessible by any other classes within your assembly (so only the exe you made the class in)
variable and property are almost the same. Property is preferred since you can set if other classes can set or get the variable (Property encapsulates the variable)
In C# it is the same, only you use Internal instead of Friend
Solution 3
private property are those property that are used by ourselves and other family member. But, public property are those property which are used by all the people of our community, society or the country.
JMK
Software developer currently living in Belfast, jack of a couple of trades, master of none!
Updated on June 04, 2022Comments
-
JMK almost 2 years
OK so I understand that ion VB6, encapsulated properties in a class can belong to one of three categories:
- Public Property
- Friend
- Public Variable
What is the difference between these and how do these compare to public and private properties in a more modern language like C#?
-
tcarvin about 12 yearsIn VB6, Public variables defined in classes are compiled under the covers as properties. This can be seen by using a tool such as OLE View to inspect the the dll after it is compiled. They are equivelent to the "automatically implemented properties" you mentioned.
-
tcarvin about 12 yearsSorry, when I saw "In VB6 you can change a public variable into a property without breaking the clients" I thought it might give the impression to the OP that VB6 has public variables in exposed classes.
-
Deanna about 12 yearsAlso note that variables can be passed ByRef to a function and changes will work as expected. This is NOT the case for properties. I expect this was why they were split into fields and properties in .NET/IL.