Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Public Property, Friend and Public Variable in VB6

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#?

like image 875
JMK Avatar asked Mar 12 '12 15:03

JMK


3 Answers

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 projects1and 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.

like image 196
MarkJ Avatar answered Nov 25 '22 15:11

MarkJ


  • 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

like image 41
Arxae Avatar answered Nov 25 '22 15:11

Arxae


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.

like image 38
sachin karna Avatar answered Nov 25 '22 14:11

sachin karna