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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scope of an object-how to keep it alive.

Status
Not open for further replies.

olichap

Programmer
Mar 20, 2001
389
US
I have an app that creates and sets report (call it rpt1)variables in a report object.

I then set a public report (rpt2) object in my report viewer screen to equal the populated report. The viewer form loads and shows the report.

Right after I frmViewer.Show I clear some variables in rpt1 since I must loop through and create another instance of the same report where almost all the variable are the same exceping the few I modify.

The problem is that as soon as I do this, since I have set rpt2 to = rpt1, when the viewer shows and/or prints the report the reset variables are missing. I know this is because the object is being passed BYREF, but have tried passing it into a function BYVAL, then setting a different report object but to no avail.

I guess I am looking for a way to set/pass my report object to the viewer where the passed object is NOT dependant upon the original. Anyone have any ideas?

Thanks,
Oliver

 
I should have explained that you can't reference RPT1 again after you assign it to an array because you would still be modifying the same object. Better to write code to create and access the object from a "currentReport" object, store it in the array when finished then create a new report object and access from "currentReport" object.
 
I tried to do this by adding the objects to a collection, but seem to have the same problem. Shouldn't that have the same result you mention in the above example (of keeping the object variable alive)?
 
Only if you do not reuse the same object.

'EXAMPLE
Dim objW as Object
Dim colObj as Collection

Set objW = New xxxx
objW.prop = 1
colObj.Add 0,objW ' put object REFERENCE into collection
' The following modifies the object in the collection.
' There is still only one object.
objW.prop = 2

if objW Is colObj(0) then ' will always be true
'==============================
Dim objW as Object
Dim colObj as Collection

Set objW = New xxxx
objW.prop = 1
colObj.Add 0,objW ' put object REFERENCE into collection
Set objW = New xxxx ' create whole new instance (object)
' There are now two instances
' The following DOES NOT modifiy the object just stored in the collection.
objW.prop = 2
if objW Is colObj(0) then ' will always be false
 
Better explanation?
Dim obj1 as xxxx
Dim obj2 as xxxx
Set obj1 = New xxxxx ' create object instance
Set obj2 = obj1
'BOTH object variables contain a reference or "po1nter" to the same instance.
Anything done to obj2 is done to obj2 beacuse they are the same object, just two different variables that contrain a reference to the same object instance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top