Solved

An object as a parameter in ASP function

Posted on 2004-03-25
10
232 Views
Last Modified: 2008-03-06
Hi,

In ASP, I use a function which receives one parameter.
like this
---------------------------------------------------------------------------
Set Comohject = server.createobject("comobject.comclass") ---comobject is a dll
testreveiver = testFunction(Comohject )

function testFunction(byval comobjectinfunction)
    testFunction =   comobjectinfunction.dosomefunction()
end function

set Comohject  = nothing
---------------------------------------------------------------------------
My question is that the com object receives an object as parameter.
Is it good idea to use an object as a parameter in ASP? (If not, what other method I can use to pass an object to function.)
Do I have to destroy in function? (if yes, how to do?)

Thanks,
Andrew
0
Comment
Question by:Luvu
  • 6
  • 4
10 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10680625
It is perfectly fine to do so.

Fritz the Blank
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 250 total points
ID: 10680638
A few examples:

the IsObject() inherently expects an object as a parameter.

Here are a few that I use for recordset and connection objects:

sub ClearRecordSet(ByRef strRecordSetName)
    if IsObject(strRecordSetName) then
         if not strRecordSetName is Nothing Then
              if strRecordSetName.state <> 0 then
                   strRecordSetName.close
              end if
              set strRecordSetName = Nothing
         end if
    end if
end sub

sub ClearConnection(ByRef strConnectionName)
    if IsObject(strConnectionName) then
         if not strConnectionName is Nothing Then
              if strConnectionName.state <> 0 then
                   strConnectionName.close
              end if
              set strConnectionName = Nothing
         end if
    end if
end sub

So, what you are trying to do should be fine.

Fritz the Blank
0
 
LVL 1

Author Comment

by:Luvu
ID: 10681325

Thanks,
I understand your code but I still have a question.

---------------------------------------------------------------------------
Set Comohject = server.createobject("comobject.comclass") ---comobject is a dll
testreveiver = testFunction(Comohject )

function testFunction(byval comobjectinfunction)
    testFunction =   comobjectinfunction.dosomefunction()
end function

set Comohject  = nothing
---------------------------------------------------------------------------

the scope of comobjectinfunction is only in the function.
so I think it doesn't need "set comobjectinfunction = nothing"
Do you know What the scope of the object variable in a function is?
More clearly, when the object is destroyed? just after the scope? or any time after the calling.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10681369
I am not sure that I understand. If you create an object and then pass it as a parameter, the object will stay in scope until it is explicity destroyed or until the session times out. If you had created the object within the function, that would of course be quite different--the scope would be inside the function only.

FtB



0
 
LVL 1

Author Comment

by:Luvu
ID: 10681575

That's right.

Which one is right?
1. no set nothing - because the variable will be destroyed in automatically becasue the scope.
function testFunction(byval comobjectinfunction)
    testFunction =   comobjectinfunction.dosomefunction()
end function

2. use set nothing - the variable is an object so it should be destroyed manually.
function testFunction(byval comobjectinfunction)
    testFunction =   comobjectinfunction.dosomefunction()
    set comobjectinfunction = nothing
end function
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10681698
I think that it needs to be option 3:

Set Comohject = server.createobject("comobject.comclass") ---comobject is a dll

function testFunction(byval comobjectinfunction)
    testFunction =   comobjectinfunction.dosomefunction()
end function


and then at the bottom of the page once you are done with the object:

set Comohject =Nothing


The reason is that since you create the object outside of the function, it has scope until you explicitly destroy it. Destroying it inside the function is not a good idea as you may need to access the object again. If nothing else, destroying it outside of the function maintains consistancy.

FtB
0
 
LVL 1

Author Comment

by:Luvu
ID: 10681969

But the parameter uses "Byval"
so the function copied the object whcih is a different object in the object on the page level.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10682013
I haven't tried doing this with ByVal--I always use ByRef.

However, if you would like to try it the way that you are doing, and you want to find out if the object has been destroyed, you can do this:

if isObject(Comohject) then
   if not isNothing(Comohject) then
      response.write("This object still needs to be destroyed!")
   end if
end if


FtB
0
 
LVL 1

Author Comment

by:Luvu
ID: 10687210

Thank for your help
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 10687219
You're welcome and good luck.

Fritz the Blank
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using AJAX to call classic ASP function 4 93
Forcing form refresh - classic ASP 7 64
Server Timeout with Loop 6 45
Select only the top record in a left join 13 34
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now