[Gambas-user] Friend functionality

Tobias Boege taboege at ...626...
Sat Apr 29 15:05:34 CEST 2017


On Sat, 29 Apr 2017, Leon Davis wrote:
> Gambas v3.9.2 using GTK+3
> 
> In Microsoft VB there is a "Friend" designation that allows classes in the
> same project to access each others members while protecting  those members
> access from the dot notation. Example: a programmer cannot access a friend
> member by typing "MyClass.ClassMember". Is that functionality available in
> Gambas?

It's not. I think the usual replacement pattern for this in Gambas is to
declare a pair of functions:

  Public Sub _SetClassMember(Value As Type)
  Public Function _GetClassMember() As Type

The leading underscore makes the function names hidden in the IDE, but you
can still access them despite being hidden:

  MyClass._SetClassMember(x)
  Print MyClass._GetClassMember()

Of course, this offers no real protection. Anyone can use these functions
from anywhere. You just have to keep the existence of these functions a
secret between MyClass and its "friends". For example, did you know that
gb.gui.base's GridView class lets you raise its Select event at will if
you call GridView._RaiseSelect()?

Gambas has Public and Private for visibility, Read and the absence of Read
for access and Static for storage. I hope I remember correctly that Benoit
stated that he doesn't want the Gambas OO mechanisms to become as convoluted
as C++'s. This probably includes the modifiers friend, protected, virtual,
final and what have you from other languages, as well as multiple inheritance,
polymorphism (sadly) and operator overloading (which is actually available
to some extent for native components).

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list