在VB组件中使用串缓冲 - 2
2024-07-21 02:15:42
供稿:网友
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