Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass an object to a form which accepts it as a parameter?

Status
Not open for further replies.

wcglorioso

Programmer
Jul 23, 2003
30
PH
"I create a parameter object and populate it"
loParameters = CREATEOBJECT( 'Line' )
loParameters.AddProperty('cAction', 'EDIT' ) )
loParameters.AddProperty('nValue', lnKey )

"Now I call a form which expects a parameter in its INIT method and returns a status value"
DO FORM frmProduction WITH loParameters TO lnRetVal

"Here is the INIT method of the form"
LPARAMETERS oParameters
this.cAction=oParameters.cAction
this.nValue=oParameters.nValue

"A compile time error occurs telling me that oParameters is not an object"

"My solution was to make loParameters as a private object variable and remove the parameter definition from the INIT method of the form since loParameters is already scoped to this form."

"The object reference is not passed to the "

"It solved the problem but it don't seem to fit the OOP style so I am asking for enlightenment once more."

"Thanks in advance for any reply."
 
wcglorioso,

I am using VFP 7 and am unable to reproduce the error you are getting. What version of VFP are you using?

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
slighthaze,

"I am using VFP7 as well."

Was following the discussion in the book "1001 Things You Wanted to Know About Visual Foxpro 6" by Atkins,Kramek,Schummer in Chapter 5.4 (the code was incomplete but it is eay to follow so I code it accordingly).

"Here is part of the discusion:"

The code that does most of the work resides in the custom ShowMenu method and is called from the RightClick method of both. In this example, the combo's BoundColumn contains the primary key associated with the underlying data. It is assumed that the maintenance form will return the primary key after it adds a new item (If you need different functionality, code it accordingly.):

LOCAL lnRetVal, loparameters
PRIVATE pnMenuChoice
WITH This
*** Don't display the menu if we can't add or edit
IF .lAllowNew OR .lAllowEdit
*** Display the shortcut menu
pnMenuChoice = 0
DO mnuCombo.mpr
IF pnMenuChoice > 0
*** Create the parameter object and populate it
loParameters = CREATEOBJECT( 'Line' )
loParameters.AddProperty('cAction', IIF(pnMenuChoice = 1, 'ADD', 'EDIT' ) )
loParameters.AddProperty('uValue', .Value )
*** Add any optional parameters if needed
.AddOptionalParameters( @loParameters )
*** Now call the maintenance form
DO FORM ( .cForm2Call ) WITH loParameters TO lnRetVal
lnValue = IIF(lnRetVal = 0, This.Value, lnRetVal )
.Requery()
.Value = lnValue
ENDIF
ENDIF
ENDWITH

The specifics of the maintenance form obviously depend upon the table being updated. However, any maintenance form called from the combo or list box's ShowMenu method will need to accept the parameter object passed to its Init method and use the passed information to do its job. It will also need to return to the required primary key after it has successfully added a new entry. While the specific fields to be updated by this process will vary depending on the table being updated, the process itself is fairly generic. All the forms used to add and edit entries are based on the frmAddOrEdit form class provided with the sample code for this chapter. This class clearly illustrates how the process works, and you can check out Itineraries.scx to see how this maintenance form is called by the lstAddNew and cboAddNew objects.

"I suspect an inability of Visual Foxpro to accept an object reference as a parameter"

"Data types such as logical,string,and numeric are easily passed by value."

"Can we conclude the object reference can not be passed by value?"

"Got a little background on Java."

"In Java, all data are passed by value, even objects."

"Java, even C/C++ simulates pass by reference by passing object reference by value"

"Hope I gave enough infomation already"

"Please tell me if my information is still insufficient"

"Thanks for putting worth into my inquiry"
 
I suspect an inability of Visual Foxpro to accept an object reference as a parameter

Objects, when passes as parameters, are always passed by reference.

I've passed objects as parameters many times (VFP6 & VFP7), and never had the compile-time error you mention.

However, VFP is notorious for complaining at compile time if you pass an array, then reference the array, since the compiler doesn't "know" that it's going to be an array. This is just "complaining": ignore the error and it works fine.

Or, with arrays, put this inside the proc/method/func receiving the array:
EXTERNAL ARRAY paArrayParamName

and then the compiler "knows" it's an array, and stops complaining.

I don't see a similar "EXTERNAL OBJECT xxxx" command in VFP help....

.
.
.
Since you say "A compile time error occurs telling me that oParameters is not an object"

When exactly do you get this error? The error "oParameters is not an object" should be happening during Run-Time, not compile time.

I've reproduced your form in VFP6 & VFP7, and it works as expected.

Can you post the exact code in your form's Init method?

( BTW: in the line: .AddOptionalParameters( @loParameters )
the "@" is unnecessary, and perhaps can cause problems... I have a feeling that the AddOptionalParameters method is releasing the loParameters Object reference, so that when it gets to your form's init, loParameters is .NULL....

Try this: Add to your Init, immediatetely after the Parameter line:
MESSAGEBOX('oParameters is '+type('oParameters')+','+vartype(oParameters) )

Then run your form and tell us what the messagebox displays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top