首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> ASP教程
在VB组件中使用串缓冲 - 2-ASP教程,组件开发
作者:网友供稿 点击:2
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
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.


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·在ASP.NET中使用Office Web Components (OWC)创建统计图-.NET教程,数据库应用
·windows系统下jsp+mysql+tomcat的配置-JSP教程,Application/Applet
·把aspx文件编译成DLL文件-.NET教程,Asp.Net开发
·Haneng.com的简单留言板制作源程序例子-ASP教程,ASP应用
·使用VB实现邮箱自动注册(一):表单自动提交-.NET教程,VB.Net语言
·VS.NET安装指南(To菜鸟)-.NET教程,Asp.Net开发
·web下水晶报表的使用!-.NET教程,Web Service开发
·C# 静态成员和方法的学习小结-.NET教程,数据库应用
·IIS的使用-ASP教程,ASP基础
·asp讲座之二:读取通过表单发送的数据
最新文章
·photoshop鼠绘实例:浪漫夏夜壁纸_photoshop教程
·买卖中小网站交易的一些细节问题_站长心得
·七招打造最安全的windows xp操作系统_windows xp
·做google adsense最佳和最重要的要诀_网赚技巧
·上下文关联广告清单(内文广告)推荐_网赚技巧
·广告联盟,痛定思痛_网赚技巧
·insenz首批广告费发放给站长_网赚技巧
·李彦宏:三分之一时间用在寻找人才_站长访谈
·中国汽车资源网杨锁民:网络寒冬时下海_站长访谈
·做it新闻资讯网站应先学新浪_站长心得
相关主题
  • 在vb组件内调用excel2000实现gif饼图_visualbasic教程
  • 在VB组件内调用Excel2000实现GIF饼图-ASP教程,组件开发
  • 在VB组件中使用串缓冲 - 3-ASP教程,组件开发
  • 在VB组件中使用串缓冲 - 1-ASP教程,组件开发
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭