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!

Returning a value from an instantiated class 2

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
Ok, here's a weird one for you. Is there any way to return a value from a modal form that is instantiated from the CreateObject() command? I took a modal form that returns a value such as:

do form getvalue with nParam1 to nRtnValue

I created a class definition out of it which I then instantiate with:

oGetValue=CreateObject("GetValue",nParam1)
oGetValue.Show()

At this point, the modal form takes over until it closes. The Unload event returns a value...but where would that value be found? Or will I have to use a local variable defined in the calling program to pull this off?
 
From the Object Oriented point of view, what you want to do is not right. You have a class [forget that it is a form any more] and any class has a public interface to handle the private implementation of this class.
When you design you FORM as *.VCX class you are in the object oriented territory of the language, DO FORM command and all its parameters is not working.
So all you need to do b4 you set your Class-Object variable to NULL, is to read the property you want the destroy your object

Code:
oGetValue=CreateObject("GetValue",nParam1)
oGetValue.Show()
...........
...........
...........
lTRetVal=oGetValue.Property && Assuming that you properly
*// update and handle this property inside you class.
Walid Magd
Engwam@Hotmail.com
 
A bit of nitpicking, but I couldn't resist...;-)

More beautifull is to make a get method for the value you want returned.

Create a property for the value you want returned.

Store the value in this property at runtime. And access this value by calling a method from the object (f.i. Object.GetMyValue()).

Sample class definition:

**************************************************
*-- Class: cstmyclass (c:\program files\microsoft visual studio\vfp98\mylib.vcx)
*-- ParentClass: custom
*-- BaseClass: custom
*-- Time Stamp: 06/06/01 09:40:06 AM
*
DEFINE CLASS cstmyclass AS custom


Name = "cstmyclass"

*-- Stores the value I want to return.
PROTECTED cmyvalue


*-- Returns the value of cMyValue.
PROCEDURE getmyvalue
RETURN THIS.cMyValue
ENDPROC

*-- Stores a value in cMyValue.
PROCEDURE Setmyvalue
LPARAMETERS tcValue
THIS.cMyValue = tcValue
ENDPROC
ENDDEFINE
*
*-- EndDefine: cstmyclass
**************************************************

In this case the property cMyValue is protected so it can only be set from within the object and cannot unintentionally be set from outside the object directly.

HTH,
Weedz (Wietze Veld) They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best. - Steve McConnell
 
What we do to return a value is often as stated in above threads. In your case we do the following:

oGetValue=CreateObject("GetValue",nParam1)
oGetValue.Show()

*- In the OK button we hide this form
PROCEDURE cmdOk.Click()
THISFORM.HIDE()
ENDPROC

*- Once the form is hidden, the procedure continues
luRetVal = oGetValue.GetMyValue()

*- If you declare the oGetValue as a local variable it is destroyed when getting out of scope.

HTH, Weedz (Wietze Veld) They cling emotionally to code and fix development rather than choosing practices based on analytical assesments of what works best. - Steve McConnell
 
Thanks everyone, that answers my question quite clearly. I'm still fairly new to Object Oriented Programming...I've been stuck in procedural programming most of my life. Luckily, I already have the return value as a property in my form...in fact, the Unload event has a single line:

Return Thisform.RtnValue

By changing "thisform.release" to "thisform.hide", I was able to simply grab the property and be on my way. Thanks Walid and Weedz.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top