Solved

An object as a parameter in ASP function

Posted on 2004-03-25
10
230 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

20 Experts available now in Live!

Get 1:1 Help Now