Well, enough with the conceptual and preliminary set up. Let''''s get into the code of the method. In order to demonstrate all the processes that are important to a server-side DLL meant to interact with ASP files, our example code will grow somewhat large. I''''ll section off code that exemplifies specific functionality within our example method code and present topics one at a time. This means that each code section will cover a particular topic. Some code sections will be introduced without including the code context that was previous covered, while other sections will add code in an accumulative fashion.
As you can imagine, explaining code incorporating this much topical functionality can get confusing at first. That''''s why it''''s presented one mouthful at a time. Here''''s the overall structural layout of what our finished method code will look like, although we''''ll add some string concatenation optimization to this structure once it''''s covered.
Error code statement Dim and initialize variables Open a database connection and begin a transaction Get all the note titles from the database (using Rs.Fields method) Get the user-selected note text (using Rs.GetRows) Set the note style properties (using Rs.Fields method) Commit the transaction and close the database connection Construct the note title table record Construct the note text table record Send back the constructed HTML to the calling ASP file Error capture code including transaction rollbackBasic Method Code
The code covered in this initial code section is the basic skeleton of our example method, which we''''ll flesh out later.
As you can see, the code begins with the method signature, covered previously, and ends with an End Function statement. Between the beginning and ending function code statements we have two lines of code. The first declares a string and the second sends the string''''s value back to the code that called the method. This section of code can be cut and pasted into VB and successfully called, albeit without a value being returned since we haven''''t set a value to the return string variable (strReturnString) yet. To reiterate, this method is declared as a Public function, meaning that it can be called from any COM-enabled code (it''''s public information) and will return a value (it''''s a function as opposed to a Sub, which doesn''''t return a value). We have four method parameters, three of which are Optional, meaning that the calling code doesn''''t have to include them. Default values will be used for any optional parameter method that is not set by the calling code.
Basic Method Code
Public Function ShowNotes(ByVal lngNoteID As Long, _
Optional ByVal intStyleID As Integer = 0, _
Optional ByVal strURL As String = "NoteExample.asp", _
Optional ByVal strDbConnectionString As String = "NotesDSN") As String
''''///// Variable to hold string to send back to calling code
Dim strReturnString As String
''''--> METHOD CODE GOES HERE
''''///// SEND BACK STRING
ShowNotes = strReturnString
End Function
Our method parameters include a required variable that indicates which NoteID has been selected by the user from within their browser (lngNoteID), an optional variable that indicates which note-style database record to use (intStyleID), an optional variable set to the URL that we want the returning title links in the HTML table record set to (strURL - typically the same name as the ASP file calling the method), and the optional variable containing the database string used for connecting to the database (strConnectionString).
The Method''''s Error HandlingSince our method code doesn''''t do anything meaningful when called at this point, let''''s take this opportunity and add some error code that will demonstrate how a method error can be handled. In this example code, an error is generated, or raised, each time the method is called. Later we''''ll take a look at how this type of error code is typically used.
To capture and control any errors that may occur in our method, we''''ll use an On Error statement as the first line of method code. By placing "On Error GoTo ErrorCode" before any other internal method code, any error occurring within the method will skip any remaining method code and direct the flow of control to the "ErrorCode:" line of code at the bottom of the method. Since we want all the error-free code to not issue error messages, we''''ll have to place an Exit Function statement directly before the ErrorCode statement. This way if no error occurs within the method, the function will be properly ended and not run the code past the ErrorCode statement.
Adding Error Handling
文章整理:西部数码--专业提供域名注册、虚拟主机服务
Public Function ShowNotes(ByVal lngNoteID As Long, _
Optional ByVal intStyleID As Integer = 0, _
Optional ByVal strURL As String = "NoteExample.asp", _
Optional ByVal strDbConnectionString As String = "NotesDSN") As String
''''///// ERROR CODE //////////////////////////////////////////////
On Error GoTo ErrorCode
''''///// Variable to hold string to send back to calling code
Dim strReturnString As String
''''--> METHOD CODE GOES HERE
''''///// IF/THEN STATEMENT WITH THE FOLLOWING AS A POSSIBLE ERROR CONDITION
If True Then
Err.Number = 40000
Err.Source = "String indicating where error is occurring"
Err.Description = "Error description string"
Err.Raise Err.Number, Err.Source, Err.Description
End If
''''--> METHOD CODE GOES HERE
''''///// SEND BACK STRING
ShowNotes = strReturnString
''''///// EXIT FUNCTION IF NO ERROR RAISED ////////////////////////
Exit Function
''''///// CODE IF ERROR OCCURS ////////////////////////////////////
ErrorCode:
''''///// SEND BACK RAISED ERROR
Err.Raise Err.Number, Err.Source, Err.Description
End Function
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




