OOoBasic: CreateUnoListener() returns references that can only be held by GLOBAL module vars and not PRIVATE vars ?

View: New views
5 Messages — Rating Filter:   Alert me  

OOoBasic: CreateUnoListener() returns references that can only be held by GLOBAL module vars and not PRIVATE vars ?

by Jan Holst Jensen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all.

I am doing my first event handling in OOoBasic and I am doing this to
track which cell a user has currently selected:

<CODE>
  Private SelectionChangeListener as Object

  Sub StartSelectionChangeListener
    if IsNull(SelectionChangeListener) then
      SelectionChangeListener =
CreateUnoListener("XSelectionChangeListener_",
        "com.sun.star.view.XSelectionChangeListener")
     
ThisComponent.CurrentController.AddSelectionChangeListener(SelectionChangeListener)
    end if
  End Sub

  Sub StopSelectionChangeListener
    if Not IsNull(SelectionChangeListener) then
     
ThisComponent.CurrentController.RemoveSelectionChangeListener(SelectionChangeListener)
      SelectionChangeListener = Nothing
    else
      MsgBox "LISTENER NOT STARTED"
    end if
  End Sub
</CODE>

If I first run StartSelectionChangeListener and then run
StopSelectionChangeListener I get a message box because OOoBasic has
reset the SelectionChangeListener variable to Nothing after exit of
StartSelectionChangeListener (!). If I change the declaration of
SelectionChangeListener to GLOBAL instead of PRIVATE it works as intended.

I wonder why this is so ? Because I also use PRIVATE module vars to hold
dialog references, e.g.

<CODE>
  Private Dlg as Variant

  Sub ShowMyDialog
    Dlg = CreateUnoDialog(DialogLibraries.MyModule.MyDialog)
    Dlg.Execute
  End Sub

  Sub OnButtonClick
    MsgBox Dlg.Model.MyButton.Label & " was pressed."
  End Sub
</CODE>

and the result from CreateUnoDialog() survives after exit of
ShowMyDialog and so the Dlg var is valid in the button event handler
OnButtonClick. Why this difference ? I would prefer to use PRIVATE
module vars instead of GLOBAL vars if I can.

Cheers
-- Jan Holst Jensen

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: OOoBasic: CreateUnoListener() returns references that can only be held by GLOBAL module vars and not PRIVATE vars ?

by Bernard Marcelly :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Jan,

Private or Public variables live only during execution of a macro. Global
variables survive between two executions.

In your dialog example this is same. The macro ShowDialog does not exit because
Dlg.execute waits for the dialog to be closed. You can verify this by inserting
a MsgBox("Hello") just after Dlg.execute.

Regards
   Bernard


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: OOoBasic: CreateUnoListener() returns references that can only be held by GLOBAL module vars and not PRIVATE vars ?

by Jan Holst Jensen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bernard Marcelly wrote:
> Hi Jan,
>
> Private or Public variables live only during execution of a macro.
> Global variables survive between two executions.
>
> In your dialog example this is same. The macro ShowDialog does not
> exit because Dlg.execute waits for the dialog to be closed. You can
> verify this by inserting a MsgBox("Hello") just after Dlg.execute.

Hi Bernard.

Ah - thanks. That explains it perfectly.

This behavior is not exactly clear from the Help description - here from
the Help text about the "Dim Statement":

"Dim declares local variables within subroutines. Global variables are
declared with the PUBLIC or the PRIVATE statement."

So we have global variables that do not hold data globally but only
declare the variable globally. Strange notion... but of course useful
when used in nested calls like I do in the dialog example code I listed.

Would be nice if the help described the lifecycle of variable content as
well as the declaration scope.

Cheers
-- Jan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: OOoBasic: CreateUnoListener() returns references that can only be held by GLOBAL module vars and not PRIVATE vars ?

by Bernard Marcelly :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi again, Jan,

Message de Jan Holst Jensen  date 2009-09-05 22:10 :

>
> This behavior is not exactly clear from the Help description - here from
> the Help text about the "Dim Statement":
>
> "Dim declares local variables within subroutines. Global variables are
> declared with the PUBLIC or the PRIVATE statement."
>
> So we have global variables that do not hold data globally but only
> declare the variable globally. Strange notion... but of course useful
> when used in nested calls like I do in the dialog example code I listed.
>
> Would be nice if the help described the lifecycle of variable content as
> well as the declaration scope.

Read the Basic Programming Guide, on the Wiki.
<http://wiki.services.openoffice.org/wiki/Documentation/BASIC_Guide/Scope_of_Variables>
The Basic Programming Guide is not up-to-date but is still valuable for learning
the basics of Basic and API.

Regards
   Bernard


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: OOoBasic: CreateUnoListener() returns references that can only be held by GLOBAL module vars and not PRIVATE vars ?

by Jan Holst Jensen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bernard Marcelly wrote:

> Hi again, Jan,
>
> Message de Jan Holst Jensen  date 2009-09-05 22:10 :
>>
>> This behavior is not exactly clear from the Help description - here
>> from the Help text about the "Dim Statement":
>>
>> "Dim declares local variables within subroutines. Global variables
>> are declared with the PUBLIC or the PRIVATE statement."
>>
>> So we have global variables that do not hold data globally but only
>> declare the variable globally. Strange notion... but of course useful
>> when used in nested calls like I do in the dialog example code I listed.
>>
>> Would be nice if the help described the lifecycle of variable content
>> as well as the declaration scope.
>
> Read the Basic Programming Guide, on the Wiki.
> <http://wiki.services.openoffice.org/wiki/Documentation/BASIC_Guide/Scope_of_Variables>
>
> The Basic Programming Guide is not up-to-date but is still valuable
> for learning the basics of Basic and API.
>
> Regards
>   Bernard

Much better information than the Help file - thanks. I added a note at
the end of that Wiki page about the scope gotcha.

Cheers
-- Jan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...