Are structures private to procedures?

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Are structures private to procedures?

Post by Dude »

Consider this code:

Code: Select all

Procedure Mouse1()
  GetCursorPos_(mouse.POINT)
  ProcedureReturn mouse\x
EndProcedure

Procedure Mouse2()
  GetCursorPos_(mouse.POINT)
  ProcedureReturn mouse\x
EndProcedure
Are both "mouse\x" values considered different to each other? The manual says that "Protected" only works with variables, arrays, lists and maps (not structures); so I'm assuming both "mouse\x" values are private to their own procedures? I'm calling GetCursorPos_() in different procedures in different threads.

Reason for asking: A user keeps telling me that my app won't report the correct mouse position for him (it returns null, not even "0"). So I don't know what's going on.
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Are structures private to procedures?

Post by Bisonte »

Dude wrote:

Code: Select all

GetCursorPos_(mouse.POINT)
MSDN to GetCursorPos wrote:

Code: Select all

Syntax : BOOL WINAPI GetCursorPos( _Out_ LPPOINT lpPoint );

Parameters : 

lpPoint [out]
    Type: LPPOINT
    A pointer to a POINT structure that receives the screen coordinates of the cursor.

Code: Select all

Procedure Mouse1()
  Protected mouse.POINT
  GetCursorPos_(@mouse)
  ProcedureReturn mouse\x
EndProcedure
Maybe this solves.
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Are structures private to procedures?

Post by Dude »

Code: Select all

Protected mouse.POINT
Yeah, I might have to add "Protected" and see what the user says. I didn't bother since the manual doesn't mention "Protected" for structures. Nothing to lose by trying. :)
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Are structures private to procedures?

Post by TI-994A »

Dude wrote:...I'm assuming both "mouse\x" values are private to their own procedures?
Yes. Like all other variable types, structure variables are scope-sensitive as well.

Furthermore, the Protected directive is only meant to override variable names declared elsewhere with the Global directive.

Code: Select all

localInt = 1
Global globalInt = 2

localPoint.Point\x = 3
Global globalPoint.Point\x = 4

Procedure procValues()
  localInt = 10
  Protected globalInt = 20
  
  localPoint.Point\x = 30
  Protected globalPoint.Point\x = 40  
  
  Debug localInt
  Debug globalInt
  Debug localPoint\x
  Debug globalPoint\x
  
EndProcedure

procValues()
Debug localInt
Debug globalInt
Debug localPoint\x
Debug globalPoint\x
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Are structures private to procedures?

Post by ts-soft »

The structuredefinition is always global, a compilerdirective!
This have nothing to do with the variable you are using with this structure.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Are structures private to procedures?

Post by TI-994A »

ts-soft wrote:The structuredefinition is always global, a compilerdirective!
Yes; even if it's defined within procedures that are not called.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply