Issue 17948 - Basic: private variables are public ?
Summary: Basic: private variables are public ?
Status: CLOSED FIXED
Alias: None
Product: udk
Classification: Code
Component: code (show other issues)
Version: OOo 1.1 Beta2
Hardware: Other All
: P3 Trivial (vote)
Target Milestone: OOo 2.0
Assignee: ab
QA Contact: issues@udk
URL:
Keywords:
: 22543 (view as issue list)
Depends on:
Blocks:
 
Reported: 2003-08-07 00:02 UTC by paolomantovani
Modified: 2005-01-31 13:12 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments
Proof that Private still does not work (7.98 KB, application/vnd.sun.xml.writer)
2005-01-23 19:15 UTC, bmarcelly
no flags Details

Note You need to log in before you can comment on or make changes to this issue.
Description paolomantovani 2003-08-07 00:02:41 UTC
REM  *****  BASIC  ***** 
 
'the C variable is defined as _private_ in another module 
'strangely, the code below doesn't raises any errors 
'(notice the option explicit flag) 
 
option explicit 
 
Sub test 
 
	c = 20 
End Sub
Comment 1 kay.ramme 2003-08-07 10:41:54 UTC
Andreas, please take care of this and target appropriately.
Comment 2 aviret 2003-08-27 08:02:37 UTC
Here as small example:

MainModule:
-------------------------------------------------
REM  *****  BASIC  *****
REM      Main Module

Option Explicit

Private iMainModuleInit As Integer ' Private != Global

Sub Main
	ExamplePrivate()
	ExamplePrivate()
End Sub

Sub ExamplePrivate
	if iMainModuleInit = 0 then iMainModuleInit = InitVar() REM Test if
variable is already initialized
		iMainModuleInit = iMainModuleInit + 1
	MsgBox iMainModuleInit,0,"The answer is"
End Sub

REM Function for initialization of the static variable
Function InitVar() As Integer
	InitVar = 40 REM any value for initialization
End Function
-----------------------------------------------

Yet Another Module, call it for instance TestModule:
-----------------------------------------------
REM  *****  BASIC  *****
REM      Test Module

Option Explicit

Sub Main
	' The variable iMainModuleInit is declared/defined in the MainModule
	' as Private, but it's accessible from this module...

	ExamplePrivate()
		
	iMainModuleInit = 100
	
	ExamplePrivate()
End Sub
-----------------------------------------------

The documentation says that the Private variable should be usable for
the module where it is declared, but the second module could use it
and set some values to this private variable.

Another point is also that the Private variable should be Global, but
that's not the case, with the Private reserved word, you will lost the
Global property of the variable. Do we have to open another issue
about this problem ?
Comment 3 aviret 2003-08-27 08:05:44 UTC
It's the same behavior with SO 6.1 Beta2 and OOo 1.1rc3.
Comment 4 andrew 2003-08-27 14:47:21 UTC
Use Private to declare a variable in a module that should not be 
visible in another module (possible bug). Private variables, like 
Public variables, are initialized every time that a macro is run. 

A single variable name may be used by two different modules as their 
own variable if the variable is declared Private. This is the 
advantage of Private
Comment 5 ab 2003-09-01 14:19:59 UTC
-> OOo 2.0, but will probably be fixed earlier, because there's 
already an interal task for this.
Comment 6 ab 2003-09-03 15:47:32 UTC
Forgot to set to OOo 2.0
Comment 7 ab 2004-02-04 12:49:24 UTC
*** Issue 22543 has been marked as a duplicate of this issue. ***
Comment 8 ab 2004-03-24 15:10:50 UTC
Since OOo 1.1.1 variables declared Private/Dim on module level are really private,
but only if the new compiler Option Compatible is used:

' Module 1
Option Compatible
Private b$    ' This variable is really private
Dim a           ' This too

The reason for this condition is backward compatiblity. If Private/Dim on module
level became really private it could break existing programs. While this may be
acceptable for the Private command whose semantic is very obvious by its name,
it's not acceptable for Dim that could have easily been used in a wrong way. Pri-
vate and Dim are completely identical, so both could only be changed for Option 
Compatible.
Comment 9 ab 2004-10-13 09:48:25 UTC
Closed
Comment 10 bmarcelly 2005-01-23 19:13:03 UTC
I made a test using Option Explicit, see my next attachment :
Private is still not Private in Ooo 1.1.3 and in OOo 1.9 m65.
This issue should be reopened.
Comment 11 bmarcelly 2005-01-23 19:15:34 UTC
Created attachment 21786 [details]
Proof that Private still does not work
Comment 12 ab 2005-01-31 12:29:38 UTC
It's very simple, my example is wrong. The private state does not depend on the
"Option Compatible" but on the compatibility runtime mode. CompatibilityMode
( true )". This looks strange - and that's why I ran into my own trap I guess - but 
that's how it's realised. OOo Basic always searches for symbols at runtime and
only decides after finding the private symbol in another module that this symbol
is private and cannot be accessed. Solving this on compiler level would have re-
quired major changes in the Basic core that I did not want to do.

So this is the correct code:

REM  *****  Module1  *****
Private myText As String

Sub initMyText
	myText = "HellOoo"
	print "in module1 : ", myText
End Sub

REM  *****  Module2  *****
'Option Explicit

Sub demoBug
	CompatibilityMode( true )
	initMyText
	
	' Now returns empty string
	' (or rises error for Option Explicit
	print "Now in module2 : ", myText
End Sub

Comment 13 bmarcelly 2005-01-31 13:12:27 UTC
OK, it works now. 
I understand that CompatibilityMode(true) sets an internal parameter, and it can be set 
(or reset) anytime during execution.

Still a problem : this is not documented in the on-line help, not even on 1.9 (checked 
on 1.9m65).