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

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

时间: 2024-10-01 05:57:22

在VB组件中使用串缓冲 - 2的相关文章

在VB组件中使用串缓冲 - 1

Lesson 1 : Overview -------------------------------------------------------------------------------- This article serves as a quick "How To" example for using string buffering to concatenate strings in a VB component. The example VB code will ge

在VB组件中使用串缓冲 - 3

Lesson 3 : The ASP File -------------------------------------------------------------------------------- How2Project4.asp The Asp CodeWe can now use our component from any asp page. Here we set our connection string, the sql statement, and the number

应用-VB代码中com+组件安装问题

问题描述 VB代码中com+组件安装问题 '函数名称:AddComponent '作用:添加组件到com+对应应用中 '参数:sFolder COM+目录,sSourcePath源文件夹路径,sTargetPath目标文件夹路径,sFileName组件名 Public Function AddComponent(sComFolder As String, ByVal sTargetPath As String, sFileName As String) As Boolean Dim oo As

VB.NET中的组件开发

组件开发 先看段组件的代码:(临时写的,写得比较乱) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '' 登录验证组件 '' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Imports System.Security.CryptographyImports System.TextImports System.DataImpo

VB.NET中的多线程开发

多线程 引言 对于使用VB6的开发者而言,要在程序中实现多线程(multi-thread)功能,一般就是使用Win32 API调用.但凡是进行过这种尝试的开发者都会感觉到实现过程非常困难,而且总是会发生些null terminated strings GPF的错误.可是有了VB.NET,一切烦恼都成为过去. 自由线程(free threaded) 在VB6中,我们只能对组件设置多线程模式,这通常就是单元模式的多线程.对于单元线程组件而言,组件中的每个可执行方法都将在一个和组件相联系的线程上运行.

VB.NET中声音的播放 Montaque(原作)

VB.NET中声音的播放    Montaque(原作)          由VB6升级为.NET后,有些人不清楚声音的处理,比如程序出错的时候,自定义一个声音播放,或者程序的背景音乐.包括游戏音乐等等.下面介绍几种在VB.NET中计较简单可以实现的方案: 1.  Beep 最简单的一种方法,通过计算机的扬声器发出声响, 声响的音高与持续时间取决于硬件和系统软件,从而随计算机不同而不同. Beepg跟Msgbox等方法位于Microsoft.VisualBasic.Interaction 中,一

在VB.NET中进行抓屏

'Author:wgscd '功能:抓屏 'QQ153964481 'Date:2005-4-12 '*********************************  Public Class Form1     Inherits System.Windows.Forms.Form #Region " Windows 窗体设计器生成的代码 "     Public Sub New()         MyBase.New()         '该调用是 Windows 窗体设计器所

把握VB.NET中的流(Stream)

stream 当你第一次用VB.NET读写文件的时候,你肯定会发现VB.NET摒弃了传统的文件I/O支持,感觉不习惯.其实,在.NET里面,微软用丰富的"流"对象取代了传统的文件操作,而"流",是一个在Unix里面经常使用的对象.我们可以把流当作一个通道,程序的的数据可以沿着这个通道"流"到各种数据存储机构(比如:文件,字符串,数组,或者其他形式的流等).为什么我们会摒弃用了那么久的IO操作,而代之为流呢?其中很重要的一个原因就是并不是所有的数据

VB.NET中声音的播放

由VB6升级为.NET后,有些人不清楚声音的处理,比如程序出错的时候,自定义一个声音播放,或者程序的背景音乐.包括游戏音乐等等.下面介绍几种在VB.NET中计较简单可以实现的方案: 1.  Beep 最简单的一种方法,通过计算机的扬声器发出声响, 声响的音高与持续时间取决于硬件和系统软件,从而随计算机不同而不同. Beepg跟Msgbox等方法位于Microsoft.VisualBasic.Interaction 中,一般默认系统会自动加载.调用很简单,看下面的例子. Dim I As Inte