Discussion:
VB6 - Initializing a variable
(too old to reply)
Andrew Chalk
2006-07-22 21:37:16 UTC
Permalink
If 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().

Many thanks,
Thorsten Albers
2006-07-22 22:00:09 UTC
Permalink
Post by Andrew Chalk
If 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
----------------------------------------------------------------------
Jim Mack
2006-07-22 23:08:18 UTC
Permalink
Post by Thorsten Albers
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};).
Dim MyStruct As MyStructTemplate
Dim MyStructEmpty As MyStructTemplate
... ' code
MyStruct = MyStructEmpty
... ' more code
This is really the only good way of re-initializing a UDT variable. While splashing 0's all over the structure does set the members to null etc, it does nothing for any object, array or VL string members of the Type. Any of those in the variable are orphaned (object reference counts are not decremented, strings are not deallocated, and arrays, which may also contain these things, are abandoned in memory).

If the UDT is always simple numerics / FL strings, then blast away.

Another benefit of using a template variable is that fields can be set to default non-null values instead of always being null.
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
Thorsten Albers
2006-07-22 23:36:07 UTC
Permalink
Post by Jim Mack
This is really the only good way of re-initializing a UDT variable.
While
Post by Jim Mack
splashing 0's all over the structure does set the members to null etc, it
does nothing for any object, array or VL string members of the Type. Any
of those in the variable are orphaned (object reference counts are not
decremented, strings are not deallocated, and arrays, which may also
contain these things, are abandoned in memory).
In general I agree. But there are some rare cases where it is necessary to
set the contents of the type to 0/Null (ZeroMemory()) in order to prevent
VB from freeing the memory allocated for one or more of the type members
when exiting the scope of the type. Such cases of course are mainly
restricted to the use of external APIs.
--
----------------------------------------------------------------------
THORSTEN ALBERS Universität Freiburg
albers@
uni-freiburg.de
----------------------------------------------------------------------
Andrew Chalk
2006-07-23 20:56:33 UTC
Permalink
Thanks all for your help. It appears that I can assume that a UDT is
initialized to all NULs by VB.

- A
Post by Thorsten Albers
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};).
Dim MyStruct As MyStructTemplate
Dim MyStructEmpty As MyStructTemplate
... ' code
MyStruct = MyStructEmpty
... ' more code
This is really the only good way of re-initializing a UDT variable. While
splashing 0's all over the structure does set the members to null etc, it
does nothing for any object, array or VL string members of the Type. Any of
those in the variable are orphaned (object reference counts are not
decremented, strings are not deallocated, and arrays, which may also contain
these things, are abandoned in memory).

If the UDT is always simple numerics / FL strings, then blast away.

Another benefit of using a template variable is that fields can be set to
default non-null values instead of always being null.
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
Tony Proctor
2006-07-24 07:54:13 UTC
Permalink
A variation of 'MyStruct = MyStructEmpty', that I personally like, it to
make MyStructEmpty into a Function returning the UDT template instead of
just a variable. The Function can then be parameterised, thus giving a sort
of primitive UDT "constructor", e.g.

MyStruct = MyStructEmpty (UserID:=12)

Yeah, yeah, I know, best to use classes from the start. I'm not going
there.... today

Tony Proctor
Post by Thorsten Albers
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};).
Dim MyStruct As MyStructTemplate
Dim MyStructEmpty As MyStructTemplate
... ' code
MyStruct = MyStructEmpty
... ' more code
This is really the only good way of re-initializing a UDT variable. While
splashing 0's all over the structure does set the members to null etc, it
does nothing for any object, array or VL string members of the Type. Any of
those in the variable are orphaned (object reference counts are not
decremented, strings are not deallocated, and arrays, which may also contain
these things, are abandoned in memory).

If the UDT is always simple numerics / FL strings, then blast away.

Another benefit of using a template variable is that fields can be set to
default non-null values instead of always being null.
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
J French
2006-07-23 09:46:19 UTC
Permalink
On Sat, 22 Jul 2006 16:37:16 -0500, "Andrew Chalk"
Post by Andrew Chalk
If 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().
Don't, if your UDT holds anything fancy like variable length Strings,
you'll create havoc

Dim MyUDT As TUDT

......

Dim TmpUDT As TUDT ' create 'empty' UDT

MyUDT = TmpUDT ' wallop MyUDT
Bob O`Bob
2006-07-23 20:27:50 UTC
Permalink
Post by Andrew Chalk
If 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().
Many thanks,
If you really mean "initialize" then I believe you're simply wasting your time.

But if you mean REinitialize, then the best way is probably to create one
instance of that type, which your code *never* alters, and use LSet to
copy the values from it.

If you actually want to set every element (regardless of constituent type) to
zero, then the advice I've seen already posted will probably do that just fine.
Though I can't imagine why anyone would actually want *that* in VB.



Bob
--
Continue reading on narkive:
Loading...