OK so I understand that ion VB6, encapsulated properties in a class can belong to one of three categories:
What is the difference between these and how do these compare to public and private properties in a more modern language like C#?
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With