[Solved] Module Variable Scope
[Solved] Module Variable Scope
I'm new to LibreOffice BASIC and downloaded the OpenOffice BASIC Guide 3.0.0. On page 22 is says "Private variables are only available in the module in which they are defined."
But I can access private variables across modules without errors.
Module1:
Private var1 as Integer
Module2:
Module1.var1 = 3
msgbox Module1.var1 ( returns 3 )
An old post named "[BASIC] Object-Oriented Programming" at "Board index ‹ Customizing and Extending ‹ Code Snippets" provided an excellent example of the ClassModule definition. A comment made in a reply seemed to suggest that inadvertent access to another module's private variable can be halted through use of the OOP class GET/LET/SET properties (defined as Public).
"the point is you can use the variable name _X in another module and you won't inadvertantly change the value in the class module because its private. You need to use Let and Get to set it."
But if _X is defined as Private in another module you shouldn't be able to access it inadvertently?
I'm confused. Is there a property that I'm not setting that enforces scope restrictions across modules or is it a LibreOffice problem or am I going mad? (don't answer the "mad" bit)
But I can access private variables across modules without errors.
Module1:
Private var1 as Integer
Module2:
Module1.var1 = 3
msgbox Module1.var1 ( returns 3 )
An old post named "[BASIC] Object-Oriented Programming" at "Board index ‹ Customizing and Extending ‹ Code Snippets" provided an excellent example of the ClassModule definition. A comment made in a reply seemed to suggest that inadvertent access to another module's private variable can be halted through use of the OOP class GET/LET/SET properties (defined as Public).
"the point is you can use the variable name _X in another module and you won't inadvertantly change the value in the class module because its private. You need to use Let and Get to set it."
But if _X is defined as Private in another module you shouldn't be able to access it inadvertently?
I'm confused. Is there a property that I'm not setting that enforces scope restrictions across modules or is it a LibreOffice problem or am I going mad? (don't answer the "mad" bit)
Last edited by simH on Fri Jun 30, 2017 5:45 am, edited 3 times in total.
LibreOffice 5.1.6.2 on Ubuntu 16.04LTS
Re: Module Variable Scope
It seems work only because the 'Module1.var1' will be a LOCAL variable name in the Module2, but not a reference to the Module1. (In other words: It seems, if you can use the dot character in a NAME of a local variable, and the Module1.var1 = 3 command will not modify the value of the Private-scope variable named var1 of the Module1.)But I can access private variables across modules without errors.
Module1:
Private var1 as Integer
Module2:
Module1.var1 = 3
msgbox Module1.var1 ( returns 3 )
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Module Variable Scope
Ok I see your point. But then in this example Module2 accesses Module3 private var EmpNumber through the public function and directly.
Module2:
Sub Main
Module3.PutEmp(15)
MsgBox Module3.GetEmp() ... returns 15 as expected
MsgBox Module3.EmpNumber ... returns 15 ????
End Sub
Module3:
Option Explicit
Private EmpNumber as Integer
Public Sub PutEmp( ByVal inEmp as Integer)
EmpNumber = inEmp
End Sub
Public Function GetEmp() as Integer
GetEmp() = EmpNumber
End Function
Module2:
Sub Main
Module3.PutEmp(15)
MsgBox Module3.GetEmp() ... returns 15 as expected
MsgBox Module3.EmpNumber ... returns 15 ????
End Sub
Module3:
Option Explicit
Private EmpNumber as Integer
Public Sub PutEmp( ByVal inEmp as Integer)
EmpNumber = inEmp
End Sub
Public Function GetEmp() as Integer
GetEmp() = EmpNumber
End Function
LibreOffice 5.1.6.2 on Ubuntu 16.04LTS
Re: Module Variable Scope
Please upload your .ods type example file with the embedded macros here.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Module Variable Scope
Code: Select all
Private iModuleVar As Integer
Function getModuleVar() As Integer
getModuleVar = iModuleVar
End Function
Sub setModuleVar(i As Integer)
iModuleVar = i
End Sub
Code: Select all
Sub Main()
myInt = OtherModule.getModuleVar()
do_something myInt
OtherModule.setModuleVar(myInt)
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Module Variable Scope
Thanks for the help.
- Attachments
-
- TestMods.ods
- (11.06 KiB) Downloaded 139 times
LibreOffice 5.1.6.2 on Ubuntu 16.04LTS
Re: Module Variable Scope
I received the following response from JPL to my post under [Basic]Object-Oriented Programming. I suspect this applies not only to a "Class" variable but generally.
"Indeed setting the Private attribute to a class internal variable should forbid to access its value both for reading or for writing.
However the Private and Public attributes are simply ignored in StarBasic, independently from what they're applied to: class variables, module variables, Subs, Functions, they're all public even when the Private keyword is present. This has in fact as good as nothing to do with the OO programming facilities in StarBasic."
"Indeed setting the Private attribute to a class internal variable should forbid to access its value both for reading or for writing.
However the Private and Public attributes are simply ignored in StarBasic, independently from what they're applied to: class variables, module variables, Subs, Functions, they're all public even when the Private keyword is present. This has in fact as good as nothing to do with the OO programming facilities in StarBasic."
LibreOffice 5.1.6.2 on Ubuntu 16.04LTS
Re: Module Variable Scope
There is a problem with ordinary modules in that OO looks for a variable with the EmpNo name. In your example the first one it finds is EmpNo in Library 1 -- so that is EmpNo as far as your main module is concerned - as opposed to the EmpNo in Library 2.
Public/Private has no effect. The workaround is to define a variable EmpNo which it finds first so the one you want to protect isn't affected. In the main module perhaps.
Its not a problem in class modules because the only way to change the variable is to refer to the instance of the class. If you use a variable with the same name in your main module then OO doesn't find the variables in class instances when it looks for a variable with that name - I've modified your document to show this.
Public/Private has no effect. The workaround is to define a variable EmpNo which it finds first so the one you want to protect isn't affected. In the main module perhaps.
Its not a problem in class modules because the only way to change the variable is to refer to the instance of the class. If you use a variable with the same name in your main module then OO doesn't find the variables in class instances when it looks for a variable with that name - I've modified your document to show this.
- Attachments
-
- TestMods.ods
- (12.23 KiB) Downloaded 153 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Module Variable Scope
@JeJe I'm not so sure.
"the first one it finds is EmpNo in Library 1 -- so that is EmpNo as far as your main module is concerned - as opposed to the EmpNo in Library 2."
If that was the case I would get 25 for both, as it was that last assignment to EmpNo. But there are two distinct and separate EmpNo values.
Sub Main
Library1.EmpModule.PutEmpNo(15)
Library2.EmpModule.PutEmpNo(25)
MsgBox Library1.EmpModule.GetEmpNo() ' = 15
MsgBox Library2.EmpModule.GetEmpNo() ' = 25
End Sub
It's a bit disappointing to find that such an important construct/behaviour of the language is simply missing while all the reference manuals assert otherwise.
"the first one it finds is EmpNo in Library 1 -- so that is EmpNo as far as your main module is concerned - as opposed to the EmpNo in Library 2."
If that was the case I would get 25 for both, as it was that last assignment to EmpNo. But there are two distinct and separate EmpNo values.
Sub Main
Library1.EmpModule.PutEmpNo(15)
Library2.EmpModule.PutEmpNo(25)
MsgBox Library1.EmpModule.GetEmpNo() ' = 15
MsgBox Library2.EmpModule.GetEmpNo() ' = 25
End Sub
It's a bit disappointing to find that such an important construct/behaviour of the language is simply missing while all the reference manuals assert otherwise.
LibreOffice 5.1.6.2 on Ubuntu 16.04LTS
Re: Module Variable Scope
Code: Select all
Sub Main
EmpNo = 10
MsgBox Library1.EmpModule.GetEmpNo() ' = 10
MsgBox Library2.EmpModule.GetEmpNo() ' = 0
End Sub
If you're looking for faults with OO... things not implemented fully as in say VB6... well, there are many things. There are also things you can do with it that Microsoft Office doesn't let you do.It's a bit disappointing to find that such an important construct/behaviour of the language is simply missing while all the reference manuals assert otherwise.
OO is a free Office Suite to use or not... Have you identified a real problem in the sense of no workaround to it? No. Don't use generic names for variables when there's a risk of calling them elsewhere - choose a unique name so you'll only call it when you mean to... just be careful... No real problem.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Module Variable Scope
@JeJe
Ok thanks mate. I concur with your comments about picking up the first occurrence of a variable. It appears that it does.
Now your attitude.
My very first ask for help on this forum was aggressively dismissed by you as "picking fault" along with a bit of advice about old threads.
"What you're picking fault with is how its designed - the point is you can use the variable name _X in another module and you won't inadvertantly (sic) change the value in the class module because its private. You need to use Let and Get to set it.
Its not that great an idea to ask questions on the bottom of a 5 year old thread."
It was thanks to JPL, who quickly managed to determine what my problem was and professionally deal, that the matter was resolved.
Now on this post your contribution is to re-iterate what JPL has already said adding the suggestion that I'm "looking for faults with OO". You go on to offer some gratuitous advice "OO is a free Office Suite to use or not" clearly suggesting that if I don't like OOBasic then I should bugger off back to VB6.
Do me a favour. Ignore my posts.
Ok thanks mate. I concur with your comments about picking up the first occurrence of a variable. It appears that it does.
Now your attitude.
My very first ask for help on this forum was aggressively dismissed by you as "picking fault" along with a bit of advice about old threads.
"What you're picking fault with is how its designed - the point is you can use the variable name _X in another module and you won't inadvertantly (sic) change the value in the class module because its private. You need to use Let and Get to set it.
Its not that great an idea to ask questions on the bottom of a 5 year old thread."
It was thanks to JPL, who quickly managed to determine what my problem was and professionally deal, that the matter was resolved.
Now on this post your contribution is to re-iterate what JPL has already said adding the suggestion that I'm "looking for faults with OO". You go on to offer some gratuitous advice "OO is a free Office Suite to use or not" clearly suggesting that if I don't like OOBasic then I should bugger off back to VB6.
Do me a favour. Ignore my posts.
LibreOffice 5.1.6.2 on Ubuntu 16.04LTS
Re: [Solved]Module Variable Scope
Nothing even remotely like that.suggesting that if I don't like OOBasic then I should bugger off back to VB6.
I've done nothing but try to help and inform you as best I can in both threads. In my last post I was telling you something you got wrong - I get nothing out of that, its all for your benefit. Other people have helped me in computer forums so this is my payback.
My responses are terse because life is short. Its very easy to see something in short posts that aren't there. But I'm perhaps the person who's spent most time here trying to help you.
If I remember your name, fine, I will not help you again. You'll find that when you post a question people don't always give you exactly what you want - but they're all taking time out to try to help you. If you then throw it back in their face as you've done here: its actually you who has been very rude.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)