Solved

An object as a parameter in ASP function

Posted on 2004-03-25
10
231 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migrated SQL connection issues 2012 6 39
Leverage browser caching 2 55
ASP Syntax for IF statement 21 58
SQL Server Web Traffic Limitations 4 54
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…
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/…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

21 Experts available now in Live!

Get 1:1 Help Now