Solved

An object as a parameter in ASP function

Posted on 2004-03-25
10
233 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…

856 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