private variable pased by ref to a method

This forum is meant for questions about the Visual FoxPro Language support in X#.

Post Reply
jpmoschi
Posts: 76
Joined: Thu May 21, 2020 3:45 pm

private variable pased by ref to a method

Post by jpmoschi »

Good morning forum, look this case
When a private variable not change by ref values inside a method but if i change the private declaration with local the problem is resolved. Remember i am testing in 2.6a

private loteprueba
loteprueba:= 10
obj:ArrobaGet(1 , 0+ len("Test Loops:") , @loteprueba, "99999999")
// loteprueba never changed !!!! But inside ArrobaGet i am sure, value is changed!!!

Thanks
Juan
User avatar
robert
Posts: 4290
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

private variable pased by ref to a method

Post by robert »

Juan,

Is obj typed ?
In other words: is this what we call an "early bound" method call or a "late bound" method call ?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

private variable pased by ref to a method

Post by Karl-Heinz »

Hi Robert,

i compiled with 2.7 an older sample that uses the Fox dialect and noticed that the caller doesn´t see a changed ref value if the Fox "=" assignment is used . The only way to make it work is to use inside the ref function/method the ":=" assignment.

The only compiler switch i´m using is the /memvar option. I also tryied /lb, but that doesn´t change the ref behaviour of the method call. Looking with ILSpy at the code shows that the ref related "=" code is different than the ":=" code.

Code: Select all

FUNCTION TestByRef( ) AS VOID
LOCAL x2 AS STRING
LOCAL o AS Foo 	

// LOCAL x AS STRING 
LOCAL x 
// PRIVATE x 


x = "old"
? x
o = Foo{}
o:TestUntyped(@x )
? "(Method) must be 'new': " , x 
?  

x = "old"
? x
TestUnTyped ( @x ) 
? "(Function) must be 'new': " , x            
? 

x = "old" 
? x  
TestUnTyped ( x )
? "(Function) must still be 'old': " , x 
? 

x = "old"
? x
DO TestUnTyped WITH x 
? "(Procedure) must be 'new': " , x 	 
? 

x = "old"
? x
DO TestUnTyped WITH (x) 
? "(Procedure) must still be 'old': " , x
? 	

x2 = "old"
? x2
TestTyped ( @x2 )
? "(Typed Func) must be 'new': " , x2   
?
RETURN 

FUNCTION TestUnTyped ( x )
// PARAMETERS x	
    
	IF IsByRef ( x )
		
		// x := "new" // This works !
		
		// note: if the Fox "=" assignment is used 
		// the caller doesn´t see the changes ?
		 
		x = "new"  
		
		
		? "ByRef: change param value" 
		
	ENDIF 	
	
	RETURN  .t.
	

FUNCTION TestTyped ( c REF STRING ) AS LOGIC 
	
	c = "new"  // no problem with the Fox "=" assignment 
	
	RETURN TRUE
	
	
CLASS Foo
	

METHOD TestUntyped ( x ) 
	
	IF IsByRef ( x )
		
		// x := "new" // This works !
		
		// note: if the Fox "=" assignment is used 
		// the caller doesn´t see the changes ?
		
		x = "new"  
		
		? "ByRef: change param value" 
		
	ENDIF 
	
	RETURN
	
END METHOD 		

END CLASS 		
regards
Karl-Heinz
User avatar
Chris
Posts: 4599
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

private variable pased by ref to a method

Post by Chris »

Hi Karl-Heinz,

I think you are testing with the newer compiler test release, not 2.7, because this seems to work fine in 2.7. But I can indeed reproduce the behavior in the new 2.8 compiler, the by ref param is assigned the new value back only when using the ;= operator, not with =. Will log a test for this, thanks for reporting it!
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

private variable pased by ref to a method

Post by Karl-Heinz »

Hi Chris,

mmmhh ...

no, i don´t use the 2.8 compiler. When i compile the posted sample the XIDE output shows version 2.7.0.0 ?

regards
Karl-Heinz
User avatar
Chris
Posts: 4599
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

private variable pased by ref to a method

Post by Chris »

Hi Karl-Heinz,

Hmm, that's strange, when I compile with 2.7 here, I get a compiler error for the FUNCTION TestTyped() declaration. If I remove this, then the code runs as expected, but only if /fox2+ is enabled (when it is disabled, I see that indeed the value is not updated).

Anyway, it's not really important what happens with 2.7, I still see some issues in 2.8 so I have logged a sample now.
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
jpmoschi
Posts: 76
Joined: Thu May 21, 2020 3:45 pm

private variable pased by ref to a method

Post by jpmoschi »

Hi Robert
Obj is defined like this
private Obj
Obj:= MyStartupCode:WinFormOn()
and WinFormOn() is a static method like this
static method WindOmaOn() as WinForm_private // and this one inherit from System.Windows.Forms.Form

Regards
Juan
User avatar
robert
Posts: 4290
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

private variable pased by ref to a method

Post by robert »

Juan,
Ok, so obj is a private memory variable. That means that:
- the call is late bound
- we cannot really pass the address of the variable. There is no location on the stack. So we must pass the value of the variable to the method and when the method returns we must assign the changed value back to the private variable.
I thought we had this working, but apparently there is a problem when the assignment in the method is done with the '=' operator and not with the ':=' operator.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply