请各位看下,为什么 这段麻烦会报内存错误
这是正确的:
Public Function AddVar(Dat() As Byte, Count As Long, ByVal Var As Variant)Dim Ty As VbVarTypeDim Vint As IntegerDim Vlng As LongDim Vsin As SingleDim Vdub As DoubleTy = VarType(Var)If Ty = vbByte Then ReDim Preserve Dat(Count) Dat(Count) = Var Count = Count + 1ElseIf Ty = vbInteger Then Vint = Var ReDim Preserve Dat(Count + LenB(Vint) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vint), LenB(Vint) Count = Count + LenB(Vint)ElseIf Ty = vbVLong Then Vlng = Var ReDim Preserve Dat(Count + LenB(Vlng) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vlng), LenB(Vlng) Count = Count + LenB(Vlng)ElseIf Ty = vbSingle Then Vsin = Var ReDim Preserve Dat(Count + LenB(Vsin) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vsin), LenB(Vsin) Count = Count + LenB(Vsin)ElseIf Ty = vbDouble Then Vdub = Var ReDim Preserve Dat(Count + LenB(Vdub) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vdub), LenB(Vdub) Count = Count + LenB(Vdub)End IfEnd Function
Public Function AddVar(Dat() As Byte, Count As Long, ByVal Var As Variant)Dim Ty As VbVarTypeDim Vint As IntegerDim Vlng As LongDim Vsin As SingleDim Vdub As DoubleTy = VarType(Var)If Ty = vbByte Then ReDim Preserve Dat(Count) Dat(Count) = Var Count = Count + 1ElseIf Ty = vbInteger Then Vint = Var ReDim Preserve Dat(Count + LenB(Vint) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vint, LenB(Vint) Count = Count + LenB(Vint)ElseIf Ty = vbVLong Then Vlng = Var ReDim Preserve Dat(Count + LenB(Vlng) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vlng, LenB(Vlng) Count = Count + LenB(Vlng)ElseIf Ty = vbSingle Then Vsin = Var ReDim Preserve Dat(Count + LenB(Vsin) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vsin, LenB(Vsin) Count = Count + LenB(Vsin)ElseIf Ty = vbDouble Then Vdub = Var ReDim Preserve Dat(Count + LenB(Vdub) - 1) CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vdub, LenB(Vdub) Count = Count + LenB(Vdub)End IfEnd Function