Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

An object as a parameter in ASP function

Posted on 2004-03-25
10
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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