Don,
Believe me I thought sooner or later this subject would turn into this:)
Private variable is visible to any routine down from where it's declared. IOW consider you have
funcA, funcB, funcC, funcD each calling one another in chain and funcX called separately.
public m.var
m.var = "hello"
? "Main",m.var
funcA()
? "funcA return",m.var
funcX()
? "funcX return",m.var
procedure funcA
private m.var
m.var = "there"
funcB()
? "funcA", m.var
procedure funcB
m.var = "again"
funcC()
procedure funcC
m.var = "from"
funcD()
procedure funcD
m.var = "VFP"
procedure funcX
m.var = "funcX"
However with object methods (like form.load, init etc) things are different. You do not call them in chain from within another. IOW:
m.var = 'hello'
do form myForm
This is a chain call, any method in myForm sees m.var and can change it. This one is not a chain call (consider m.var is not defined before a call to do form somewhere in chain):
m.var = "hello"
messagebox(m.var)
Make it visible to all form members:
this.addproperty('var',"hello")
messagebox(this.var)
In your case actually button click doesn't even see your PRIVATE frmSwitchBoard variable (try changing its name). By coincidence it works (not exactly coincidence).
frmSwitchboard.visible = .f. && This doesn't error
do form frmForm2
Try completely removing:
private frmSwitchboard
frmSwitchboard = thisform
from load. It would still run w/o error. To understand why invoke the debugger and check (depending on how you instantiate a form you get a variable pointing to that form. ie:
do form myForm.scx
do form myForm.scx
do form myForm.scx
myForm.Caption = "Hello" && scx's name points to first instance in stack
? _vfp.forms[1].caption
To keep long story short keep your switchboard's reference in public oApp (that you create in main.prg). ie:
public oApp
oApp = newobject('custom')
oApp.AddProperty('SwitchBoard')
do form mySwitchBoard NAME oApp.SwitchBoard
read events
Now application wide you could access your SwitchBoard form like:
oApp.SwitchBoard.Hide()
oApp.SwitchBoard.Show()
etc.
Cetin
>Thanks to all of you who have shared thoughts on this subject. I gave it a try in my current application and this is what I found. In the load event of my first form I place:
>
>private frmSwitchboard
>frmSwitchboard = thisform
>
>Then in a button on frmSwitchboard I place
>
>frmSwitchboard.visible = .f.
>do form frmForm2
>
>In the destroy event of frmForm2 I place
>
>frmSwitchboard.visible = .t.
>
>At this point I get an error that effectively says it can't see frmSwitchboard. Now if frmForm2 was called by frmSwitchboard, why can't frmForm2 see it?
>
>Incidentally, if I replace the PRIVATE statement with a PUBLIC one, the problem goes away.
>
>- Don
>
>>>Hi,
>>>
>>>>>I don't use any PUBLIC variables. In my Main.prg, I create a PRIVATE var,
>>>
>>>So what's the difference between a PUBLIC var and a var declared as PRIVATE at the top level ?
>>>
>>>Regards,
>>>Viv
>>
>>One difference comes to mind - goApp loses being immune to "release all" command if declared as PRIVATE. ie:
>>
>>*Main1.prg
>>PRIVATE goApp
>>*...
>>
>>*Some procedure
>>release all && goApp is released - wouldn't if declared as PUBLIC w/o extended clause
>>
>>IOW I'm with you. I declare it as PUBLIC if not using Application object instead.
>>Cetin