|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
returning objects from functions
Is there any way to return an object from a function in ASP?
I've done it a zillion times in VB, but can't figure it out in ASP. Seems to me that it shouldn't be a problem because all variables are variants (references), so ASP shouldn't care what's being passed. For example Function GetCon() dim cn, cnstring set cn = server.createobject("adodb.connection") cn.open (cnstring) GetCon = cn end function Function GetRecord dim rs set rs = server.createobject("adodb.recordset") set rs.activeconnection = GetCon() more code end function ******************* it bombs on the line where I set the recordset's active connection because it says that there is no open connection to the DB. Well DUH. Any thoughts? |
|
#2
|
|||
|
|||
|
Well the reason why you're getting the error message is simple, look here:
Function GetCon() dim cn, cnstring set cn = server.createobject("adodb.connection") cn.open (cnstring) GetCon = cn end function NOTICE that you've Dim'ed the variable cnstring but you haven't given it any value. What you can/could do is pass the connection string as a parameter to the GetCon function such as: Function GetCon(ByVal TheConnectionString) dim cn set cn = server.createobject("adodb.connection") cn.open (TheConnectionString) GetCon = cn end function One thing you could have done to trap the error would've been to put some error handling inside the GetCon() function such as: Function GetCon() On Error Resume Next dim cn, cnstring set cn = server.createobject("adodb.connection") cn.open (cnstring) GetCon = cn 'FOR DEBUG ONLY If Err.number <> 0 Then Response.Write "Number : " & Err.number & "<br>" Response.Write "Description : " & Err.Description & "<br>" Response.Write "Source : " & Err.Source & "<br>" Response.End End If end function The error will be traped if any... Hope this helps! Sincerely Vlince |
|
#3
|
||||||
|
||||||
|
Quote:
I didn't initialize it (in this example on this BBS) for expediency reasons. Quote:
The error occurs in the function call to GetCon in the GetRecord function. Passing the connection string to the GetCon function doesn't work either, because the error I'm getting occurs because the ASP doesn't recognize the GetCon function as a connection object. Quote:
I'm not worried about error trapping per se in this example, because I know what my error is. I just need to know why this function call isn't working (because it works in VB) Thanks for the help. BTW, here is my actual code: Quote:
My error is occurring in the bold line, before the Function can even be executed. |
|
#4
|
|||
|
|||
|
because of default properties setting the value of an object to another object assumes the default property. since that occurs you need a way of referencing the entire object. that is what the Set command is for. you are trying to return an object and cn is an object therefore you need to reference the entire object and not the default property.
Code:
' change GetCon = cn ' to Set GetCon = cn
__________________
Programmer's Corner |
|
#5
|
|||
|
|||
|
AAARRRGGGGHHHH
I knew that. ![]() Quote:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > returning objects from functions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|