Type AA
    AA1 As Integer
    AA2 As Integer
End TypeType BB
    BB1 As AA
    BB2 As String * 20
    BB3 As Integer
End TypeFunction S2B(T As BB) As Byte()
Dim A() As Byte
ReDim A(Len(T) - 1)
CopyMemory A(0), T, Len(T)
S2B = A
End FunctionFunction B2S(A() As Byte) As BB
Dim T As BB
CopyMemory T, A(0), Len(T)
B2S = T
End Function'这种情况是完全没有问题的.
'但是我想在以下那种情况中.Type CC
    CC1() As Integer
    CC2() As Long
End TypeType AA
    AA1 As Integer
    AA2 As Integer
End TypeType BB
    BB1 As AA
    BB2 As String * 20
    BB3 As Integer
    BB4() As AA
    BB5() As CC
    BB6(1 to 9) As AA
    BB7(1 to 20) As CC
End Type'问题出现了,无法解决
'跪求这个问题的解.
'用在发送数据和接收数据的Dim Lem1 As BB
Dim LemB() As Byte
LemB = S2B(Lem1)
SendDate LemB

解决方案 »

  1.   

    自定义变量(结构)中的动态数组在使用前必须用redim重新定义。
      

  2.   

    这个redim重新定义我也会!在书里看到过的。
      

  3.   

    open app.path & "\lem1.dat" for binary as #1put #1,,lem1redim lemb(filelen(app.path & "\lem1.dat"))
    get #1,,lemb
    SendDate LemB'如果用生保存为文件再用数组读取就可以,但是应该还有我不了解的其它方法
      

  4.   

    因为 VB 写文件操作会自动判断每个成员的类型,然后选用合适的格式保存数据。
    而 CopyMemory 是纯内存操作,不管数据的类型。如果不想自己做序列化/反序列化,读写文件也是一种可行的方案。
      

  5.   

    在用户自定义数据类型时,如果成员为动态数组,则该成员为指针。在S2B函数中使用了CopyMemory函数,该函数表示从指定地址起复制若干字节到目标地址中,由于数据类型BB包含动态数组,使用CopyMemory时,将只复制动态数组的指针(而不是实际数据),因此,要想S2B结果正确,自定义数据类型不能包含动态数组或不定义字符串。否则,必须如老鸟所说,自己做序列化和反序列化,而不是简单地使用CopyMemory函数。