[Gambas-user] New Syntax, SUPER && !comprehension(mine) Re.: SUPER example at gambasdoc

B Bruen bbruen at ...2308...
Thu May 8 15:38:43 CEST 2014


On Thu, 08 May 2014 09:00:38 -0400
Stephen <sbungay at ...3301...> wrote:

>    Looking at the thread "New syntax for using variables arguments in a 
> function call" I realized that I needed to really brush up on my 
> somewhat marginal understanding of the evolving (a good thing) GAMBAS 
> and OOP. Not fully understanding "SUPER" (I've never used it but can see 
> a use for it) I trotted off to Google and the GAMBAS documentation, 
> where much reading was done, and (as usual) more questions raised.
>    Studying the "SUPER" example at http://gambasdoc.org/hemp/lang/super, 
> I wondered at the purpose of a collection that seems not to be used but 
> once and some syntax in it's use in the example, which didn't make any 
> sense to my old brain. Below is a snippet from the example (where $cPos 
> is a collection, $aKey an Array of String Objects (I think) and Key a 
> string object;
> 
> $cPos.Clear
> $cPos[Key] = $aKey.Count
> $aKey.Add(Key)
> 
>    OK, the first line clears the collection, got it, and the last line 
> adds the contents of "String" to $aKey, got that.... but what does the 
> second line do? It looks like it is adding the count of elements in 
> $aKey to the collection $cPos... but in OOP I would use a method call 
> i.e. $cPos.Add(Key,Key). Oh, and in trying to implement the example 
> GAMBAS (3.5.3) complained that "ListBox.Add is incorrectly overridden".
> 
> -- 
> Kindest Regards
> Steve :)
> 
> 
Probably better to read the example as:
' MyListBox class
Inherits ListBox

Private $cPos As New Collection
Private $aKey As New String[]
...

' Reimplements the Add method
Public Sub Add(Text As String, Key As String, Optional Pos As Integer = -1)

  ' Adds the item to the parent list box 
  Super.Add(Text, Key, Pos)

  ' Now do some other stuff that we want in our specialised class ....like...
  If Pos < 0 Then
    $cPos.Clear
    $cPos[Key] = $aKey.Count
    $aKey.Add(Key)
  Else
    $aKey.Add(Key, Pos)
  Endif

End

The example is just trying to demonstrate an override method in the specialised class and how to use Super to get at the ancestral class method of the same name.  The importance is that MyListBox.Add() cannot use Me.Add() as that will refer to the child class (i.e. stack overflow) but we can get to the ancestral method.
-- 
B Bruen <bbruen at ...2308...>




More information about the User mailing list