Post by Andrew ChalkIf I have created a Type, with several fields, is there a way, in VB6 to
initialize all members to 0.
I am looking for the VB equivalent to the C/C++ of memset().
a) Usually for this VB developers use not a VB equivalent but an API
equivalent:
Private Declare Sub FillMemory Lib "kernel32" Alias "RtlFillMemory" _
( _
pDestination As Any, _
ByVal lByteCount As Long, _
ByVal bByte As Byte _
)
But for initialising the type there is another one:
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" _
( _
pDestination As Any, _
ByVal lByteCount As Long _
)
So:
Dim MyStruct As MyStructTemplate
Call ZeroMemory(MyStruct, Len(MyStruct)) or
Call ZeroMemory(MyStruct, LenB(MyStruct))
For completeness and future project of yours:
Private Declare Sub CopyMemory _
Lib "kernel32" Alias "RtlMoveMemory" _
( _
pDestination As Any, _
pSource As Any, _
ByVal lByteCount As Long _
)
b) In addition:
For c(++) developers unusual is this VB behaviour: Any declared variable
gets initialize to 0/'Null'. So, after
Dim MyStruct As MyStructTemplate
all members of MyStruct are 0/'Null' (cmp. c(++): RECT rc = {0};).
This allows to erase the contents of a structure very easy:
Dim MyStruct As MyStructTemplate
Dim MyStructEmpty As MyStructTemplate
... ' code
MyStruct = MyStructEmpty
... ' more code
If you like neiter a) nor b):
The VB runtime library also provides some functions comparable to memset()
Private Declare Sub PutMem1 _
Lib "msvbvm60.dll" _
( _
pMem As Any, _
ByVal bMemVal As Byte _
)
Private Declare Sub PutMem2 _
Lib "msvbvm60.dll" _
( _
pMem As Any, _
ByVal iMemVal As Integer _
)
Private Declare Sub PutMem4 _
Lib "msvbvm60.dll" _
( _
pMem As Any, _
ByVal lMemVal As Long _
)
Private Declare Sub PutMem8 _
Lib "msvbvm60.dll" _
( _
pMem As Any, _
ByVal dbMemVal As Double _
)
--
----------------------------------------------------------------------
THORSTEN ALBERS Universität Freiburg
albers@
uni-freiburg.de
----------------------------------------------------------------------