手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

The two lines of code within this If/Then process result in the following string, given that the current loop count is at 3 and it isn''''t the note that was selected by the user.


<A HREF="NoteExample.asp?ID=3>

Since the <A> tag has been constructed, we''''ll add in the note title values.
 
        ''''///// Include the note title text from the array
        strReturnString = strReturnString & vRecordArray(ARRAY_TITLE_INDEX, lngIndexCount)

We again check to see if the current note within the loop is other than the user-selected note and end the <A> tag. An optional vbCrLf is added to the string for clearer HTML source-code reading.

''''///// Include an </A> tag if not the selected NoteID
If Not lngNoteIdIndex = lngIndexCount Then strReturnString = strReturnString & "</A>" & vbCrLf

We''''ll also add a <BR> so the note titles won''''t all end up on one line within the HTML table record.

        ''''///// End the title line with a line break
        strReturnString = strReturnString & "<BR>" & vbCrLf

This HTML table ends by adding the ending two HTML style variables (strTitleFontEnd and strTitleDataEnd) to our return string.

The second HTML table record is much simpler since it basically involves adding the two HTML style variables (strTextDataStart and strTextFontStart), the variable containing the selected note text (strNoteText), and the last two HTML style variables (strTextFontEnd and strTextDataEnd).


    ''''///// Include the beginning table data and font tags
    strReturnString = strReturnString & strTextDataStart & vbCrLf
    strReturnString = strReturnString & strTextFontStart & vbCrLf
    
    ''''///// Include the note text body
    strReturnString = strReturnString & strNoteText & vbCrLf
    
    ''''///// Include the ending font table data tags
    strReturnString = strReturnString & strTextFontEnd & vbCrLf
    strReturnString = strReturnString & strTextDataEnd & vbCrLf
    
After that, the return string is ready to send back to the calling code.

    ''''///// SEND BACK CONSTRUCTED STRING /////////////////////////////
    ShowNotes = strReturnString

Optimizing by Using String Buffering

Before you look at the code that combines all the various code sections we covered, there''''s one issue that needs to be mentioned. Although the example program is not in need of optimization, you may find that concatenating many strings within looping code can noticeably slow down your method''''s efficiency. String buffer can significantly increase processing speed by creating a large string and inserting other shorter strings with the Mid$() method.

Since you can find a more complete explanation of string buffering (see "How To Utilize String Buffering Within A VB Component" at http://www.dougdean.com for an article on string buffering), I''''ll just briefly review the buffer code and demonstrate how it can be used to construct the return string used in the ShowNotes() method.

To use the string buffering technique with our example method, we''''ll need a second method. This second method, named S1 in the code below, will be used to combine all the strings and variables we concatenated in the last code section. The code below uses cryptic method and variable names (S1, S2, S3) for string buffering in order to make the code more concise. Again, refer to the string-buffering article mentioned above for further details.

This code section uses the ShowNotes() method structure, without any extraneous string-buffering code, to exemplify building a simple string by sending string fragments to the S1() method.


''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
''''                       ShowNotes CLASS
''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
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
                   
    ''''///// String buffering variables and values
    Dim S2 As String
    S2 = String$(65536, Chr(0))
    Dim S3 As Long
    S3 = 0 

    S1 S2, S3, "This is an "
    S1 S2, S3, "example of "
    S1 S2, S3, "using the string-buffering "
    S1 S2, S3, "method in place of "
    S1 S2, S3, "concatenation"


    ''''///// SEND BACK CONSTRUCTED TEXT IN BUFFER ////////////////////
    ShowNotes = Left$(S2, S3)


End Function
''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 


''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
''''                        STRING BUFFERING
'''' S1 = Method / S2 = Buffer / S3 = Buffer Length
''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
Private Sub S1(ByRef S2 As String, ByRef S3 As Long, ByVal AddString As String)
 
    Dim strTemp As String
    Dim lngLoop As Long
        
    ''''///// Empty strings will cause a fatal error if not eliminated
    If Not Trim$(AddString) = "" Then
    
        ''''[1]///// DOES BUFFER NEED TO BE INCREASED?
        If S3   Len(AddString) > Len(S2) Then
            ''''///// STORE S2
            strTemp = S2
            ''''///// Increase memory storage bytes
            Do
                lngLoop = lngLoop   1
                If (Len(S2)   (65536 * lngLoop)) >= (S3   Len(AddString)) Then
                    Exit Do
                End If
            Loop
            ''''///// Resize buffer
            S2 = String$(Len(S2)   (65536 * lngLoop), Chr(0))
            ''''///// RESTORE S2
            Mid$(S2, 1, S3) = strTemp
        End If
        
        ''''[2]///// ADD STRING TO BUFFER
        Mid$(S2, S3   1, Len(AddString)) = AddString
        ''''[3]///// SET STRING LENGTH IN BUFFER
        S3 = S3   Len(AddString)
        
    End If
    
End Sub
''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!