欢迎光临
我们一直在努力

在VB组件中使用串缓冲 – 2

建站超值云服务器,限时71元/月

lesson 2 : the vb component

——————————————————————————–

how2project4.vbp

the dobuffer() method

the dobuffer() method will be sent a string buffer (strbuffer), the value of the length of the string data

already placed within the buffer (lngbuffer), and the string to add to the buffer (addstring). these

arguments will be declared and sent from our methodname() method, which will be covered later in this

article.

private sub dobuffer(byref strbuffer as string, byref lngbuffer as long, byval addstring as string)

notice that the dobuffer() method is declared as a subroutine rather than a function. this is because it

doesn"t really return any data from the method itself. rather, it manipulates the data sent by reference

from the calling method.

two local variables need to be declared for internal use.

dim strhold as string

dim lngindex as long

now our first line of business will be to determine whether our buffer is large enough to hold the string

data we want to add to it. but before we do this, we want to check out whether our calling method sent a

null string or not.

if not trim$(addstring) = "" then

if the calling method sends a null string, we"ll quietly skip the concatenating process and avoid the

whole affair via this if-then statement. on the other hand, if the sent string holds characters, we"ll

need to check that the buffer is large enough to hold them. to do this the current length of the data in

the buffer is added to the length of the sent string and then the resulting sum is compared to the length

of the overall buffer size.

if lngbuffer + len(addstring) > len(strbuffer) then

if the buffer is large enough to fit the sent string, then the code discussed next will be skipped. this

will occur more often than not since we"ll set our string buffer to hold a large amount of data when we

declare it in the methodname() method. but lets assume that this isn"t the first time the dobuffer()

method was called and that the buffer is too small to add the sent string.

we first need to store the existing buffer data in a local string variable:

strhold = strbuffer

now we"ll go through a do-loop to exponentially increase a test of the size of the buffer. each time

through the loop a new buffer size is tested against the previous buffer size. once the stored buffer data

and the new string fit the buffer, the loop will be exited.

do

lngindex = lngindex + 1

if (len(strbuffer) + (65536 * lngindex)) >= (lngbuffer + len(addstring)) then

exit do

end if

loop

the 65536 size increase is arbitrary and you can set this number to what you think will be appropriate for

the total size of the string data you"re building.

the buffer can now be rebuilt to its new expanded size…

strbuffer = string$(len(strbuffer) & (65536 * lngindex), chr(0))

….and refilled with the stored string data that it previously held:

mid$(strbuffer, 1, lngbuffer) = strhold

notice that the string we want to add to the buffer hasn"t been added yet. we simply increased the size of

the buffer.

here"s the code that increases the buffer size if our sent string doesn"t fit into the buffer. it"s

repeated here so you can see it in one glance.

if lngbuffer + len(addstring) > len(strbuffer) then

strhold = strbuffer

do

lngindex = lngindex + 1

if (len(strbuffer) + (65536 * lngindex)) >= (lngbuffer + len(addstring)) then

exit do

end if

loop

strbuffer = string$(len(strbuffer) + (65536 * lngindex), chr(0))

mid$(strbuffer, 1, lngbuffer) = strhold

end if

once the buffering size has past our size test, or actually resized, we can add our sent string to the

data stored in the buffer.

mid$(strbuffer, lngbuffer + 1, len(addstring)) = addstring

now that the sent string (addstring) has been added to the buffer, we need to increase the lngbuffer

variable value to reflect the new length of the buffer"s string data stored in it.

lngbuffer = lngbuffer + len(addstring)

here"s a repeat of the code that adds the sent string to the buffer and increases the buffer data size

variable.

if not trim$(addstring) = "" then

"—–> code for occasionally resizing buffer goes here

mid$(strbuffer, lngbuffer + 1, len(addstring)) = addstring

lngbuffer = lngbuffer + len(addstring)

end if

mission complete! we added a string to a previously established string buffer without using the

concatenation (&) operant except when we needed to expand the size of the buffer, which should be

infrequently since we"re doing it exponentially and the original buffer size was set to a size that was

large enough for our string data.

the methodname() method

we"re creating two methods within our class. the methodname(), is very similar to the method explored in

the article "how to pass a variant array populated with a recordset from a vb component to an asp file".

since the methodname() method is fully covered in this previous article, i"ll only focus on modifications

needed for it to work with our dobuffer()method.

public function methodname(byval strdbconnectionstring as string, byval strsql as string, byval

intfieldcount as integer) as string

one parameter was added to the methodname() method, intfieldcount. this variable will hold the number of

fields we will be selecting in our sql statement. it will also be the number of table record data cells

that will be displayed in the browser.

the first variables we declare in the methodname() method will be the ones we send to the dobuffer()

method. the first variable (strbuffer) is a string that we"ll set to a determined length. the methodname()

method will call the dobuffer() method multiple times using strbuffer as it"s first method parameter. the

strbuffer variable will be used as a memory buffer for concatenating our strings. the dobuffer() method

defined this parameter as byref rather than byval so it will come back to the methodname() method altered.

after declaring strbuffer as type string, we dimension it to a specific length and filled it with null

characters.

dim strbuffer as string

strbuffer = string$(65536, chr(0))

the length you set strbuffer to should be determined by how much buffering space you anticipate needing.

we"ll set it at 65536, which is much higher than needed for this example – but reasonable for a lengthy

html table record. since the speed saved in avoiding using the & operant increases with the frequency in

which it"s used, i"ll assume that you"ll be using the dobuffer() method when you have to concatenate many

strings, i.e., when your database has enough records to make a large html table.

the second parameter (lngbuffer) of the dobuffer() method is used to keep track of the length of the data

we"ll be placing into the buffer. we declare this variable in the methodname() method as type long and set

it to zero.

dim lngbuffer as long

lngbuffer = 0

the lngbuffer variable is also set as byref by the dobuffer() method definition. this variable value will

reflect the length of the stored data within the buffer. we need this variable, like our strbuffer

variable, to be modifiable by the dobuffer() method. thus a reference to this variable is sent rather than

the value itself. it will come back to us changed.

the third, and last, parameter we"ll be sending to the dobuffer() method will hold the string we want to

concatenate to the data that"s being held in the buffer. as we build our html table record, the buffer

will accumulate the string fragments that will, when we"re done, compose our html table record. the

dobuffer() method accepts the addstring variable byval. this is done primarily because the strings you"ll

be sending to the dobuffer() method will be relatively short.

the strings we"ll be sending to our dobuffer() method will be a combination of string literals, integers

changed to strings via cstr(), and string data from our database.

as i mentioned previously, i"ll only minimally focus on the methodname() method since it"s basic workings

are covered in another article within this series. here, in a nut shell, is the methodname() code that

follows the declarations covered above.

"~~~~~ variable to hold database array

dim vrecordarray as variant

"~~~~~ set ado objects

dim ors as new adodb.recordset

dim ocmd as new adodb.command

dim oconn as new adodb.connection

"~~~~~ index variables for constructing html string

dim lngrecordcount as long

dim lngrecordindex as long

dim intfieldcount as integer

dim intfieldindex as integer

"~~~~~ open database with method argument

oconn.open strdbconnectionstring

"~~~~~ assign values to ado objects

ocmd.commandtext = strsql

ocmd.commandtype = adcmdtext

set ocmd.activeconnection = oconn

"~~~~~ open the recordset

ors.open ocmd

"~~~~~ assign recordset to variant array

vrecordarray = ors.getrows

"~~~~~ close recordset/connection

ors.close

oconn.close

"~~~~~ set objects to nothing

set ors = nothing

set ocmd = nothing

set oconn = nothing

the methodname() code above basically collects data from the same database structure used in the "how to

pass a variant array populated with a recordset from a vb component to an asp file" article. the major

difference is in the assignment of the getrows() values to a local variant variable (vrecordarray) rather

than the method name as in the last article.

once the data from the database is stored in the vrecordarray variant variable array, we"ll loop through

it within the component rather than sending it back to the asp file. to help us do this, we"ll store the

record count of this (zero based) array in a long variable (lngrecordcount).

lngrecordcount = ubound(vrecordarray, 2)

now we"re ready to start constructing our html table record with the database data. this is accomplished

with outside and inside loops. the outside loop will loop through each database record while the inside

loop will loop through each database field. each database field value will be send to the dobuffer()

method to be added to the buffer before sending the buffer data back to the asp file.

"***** outside loop *****

for lngrecordindex = 0 to lngrecordcount

dobuffer strbuffer, lngbuffer, "tr"

"***** inside loop *****

for intfieldindex = 0 to intfieldcount – 1

dobuffer strbuffer, lngbuffer, "td"

dobuffer strbuffer, lngbuffer, cstr(vrecordarray(intfieldindex, lngrecordindex))

dobuffer strbuffer, lngbuffer, "/td"

next

dobuffer strbuffer, lngbuffer, " "

" optional statement for including a linefeed in html source code

dobuffer strbuffer, lngbuffer, vbcrlf

next

the statement before the outside loop is used to send a tr tag. i have found it best to send a table

record rather than the entire table so the asp file can alter the table attributes easier.

note that the inside loop indexes from 0 to (intfieldcount – 1). this is because the vrecordarray is zero

based.

then opening and closing td tags sandwich the database data within the inside loop. this gives each table

data cell it"s own record field.

in order to make the html source code more readable, you can place a vbcrlf anywhere in the code. this

will cause a control-linefeed (line break) to occur in the html source code. the line break won"t be

displayed in the browser.

once the loops are complete, it"s a matter of cropping out the string data from our buffer with the

following statement:

methodname = left$(strbuffer, lngbuffer)

also notice that the strings we added to the buffer within the loop were sent with following structure:

buffermethodname, stringbuffervariable, bufferdatasize "string you want to add to the buffer"

which translates into the following statement using our method and variable names:

dobuffer, strbuffer, lngbuffer "string you want to add to the buffer"

i typically go one step further and simplify my method and variable names into something like this:

s1, s2, s3 "string you want to add to the buffer"

this reduces the extra text on my precious screen landscape.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在VB组件中使用串缓冲 – 2
分享到: 更多 (0)