如果dim i as long 
则 msgbox lenB(i)是正确的可是现在我dim i(1 to 100) as long
则 msgbox lenB(i())是错误的。怎样求LONG型数组I的字节大小啊??我在使用DX8的DIRECTPLAY8的AddDataToBuffer语句把数据绑定到1个字节数组才好发送过去。
AddDataToBuffer oBuf, I, LenB(I()), lOffset这样的格式是错误的。因为不能lenB(i())。怎样才能得到I数组的大小啊?必须得到I数组大小并且放到第三个 参数上才行的啊 大家帮帮忙啊
原型
AddDataToBuffer(   Buffer() As Byte, lData As Any,  lSize As Long,  lOffset As Long)Parameters
Buffer 
Byte array to which the data is to be added. 
lData 
Data that is to be added to Buffer. 
lSize 
Size, in bytes, of the data in lData. The simplest way to determine this value is to use the Microsoft® Visual Basic® LenB function. You can also use one of the values from the CONST_DPLAYBUFSIZE enumeration. 
lOffset 
Long value containing the offset, in bytes, to the location in the byte array where the data is to be added. When the function returns, this parameter will be set to the offset of the first byte following the data that has just been added. 
Res
If the byte array passed to Buffer is not large enough to hold the added data, the array will be enlarged without data loss to a sufficient size.

解决方案 »

  1.   


    '应该这样去求Long型数组的长度:Dim i(1 To 100) As Long
    MsgBox 4 * (UBound(i) - LBound(i) + 1)
      

  2.   

    你们的都是错的。我记得以前看过1个公式 现在想不到了。这个问题推而广之 不管I是什么类型 包括自定义类型

    TYPE typTest
    xxx as long
    yyy as long
    zzz as long
    end type
    dim i (100) AS typTest
    也要求出数组的总占用的字节大小。
      

  3.   

    同意tanaya,
    修改一下,
    MsgBox len(i(ubound(i)) * (UBound(i) - LBound(i) + 1)
    不过这个并不准确,因为如果里面的成员是string或object的话,用这种方法算出来都只占4个字节
    只要明白原理,怎样的东西都能求出来
      

  4.   

    你们难道忘记了么
    LONG占4个字节。那么i(1 to 100)就占4*100=400个字节了么?!
    数组I本身也是要占用字节的!!也就是说i(1 to 100)应该是400+n
    你们怎么都在想当然呢!我现在是忘记了这个公式 也找不到了 谁有的话跟我说说 谢谢。不要再不负责任的说400了 谢谢
      

  5.   

    终于找到了(附后)不过很奇怪的是:lenB可以用于自定义类型 例如
    Type typTest
    aaa(100) as byte
    bbb(100) as string*10
    ccc(100) as long
    end typTestdim I as typTestlenb(i)'正常
    ====把数组放到TYPE自定义类型里居然可以LENB。没天理 为什么单独的数组不能LENB呢??
    哦 上述的例子中自定义类型前提是:里面不能有OBJECT,不能有无定长字符串(因其是指针),不能有动态数组。============
    注意 任何数据类型的数组都需要 20 个字节的内存空间,加上每一数组维数占 4 个字节,再加上数据本身所占用的空间。数据所占用的内存空间可以用数据元数目乘上每个元素的大小加以计算。例如,以 4 个 2 字节之 Integer 数据元所组成的一维数组中的数据,占 8 个字节。这 8 个字节加上额外的 24 个字节,使得这个数组所需总内存空间为 32 个字节。包含一数组的 Variant 比单独的一个数组需要多 12 个字节。
      

  6.   

    directplay8的adddatatobuffer真TMD不好用
      

  7.   

    VB数组是从COM中继承下来的自动化类型,即在数组头部使用了SAFEARRAY结构。其实大多数情况下,根本不必管它!因为对于最常见的各类外部调用(如API)来说,并不支持自动化类型,所以传递数组,只是传递实际数据,即用Byref传递Array(LBound)的地址,长度当然也就是数组实际数据的字节数!
      

  8.   

    dim j as Integer
    dim s as integerfor j=1 to ubound(i)
      s=s + Lenb(StrConv(i(j), vbFromUnicode))
    next j
    msgbox s
      

  9.   

    数组中的头不用去管它,就是那20字节,如果你计算时不是按字节长度进行传递。那你得到的结果是
    错误的,因为你传递一个byte(0),那么将指向实际的地址,而不是那20字节前。
    字定义格式用len来取得实际长度,我用API传递就是这样,copymemory。