 |
|
|
|
| Author |
Message |
Andrew Chalk
Joined: 04 Oct 2007 Posts: 15
|
Posted: Sat Jul 22, 2006 8:37 pm Post subject: VB6 - Initializing a variable |
|
|
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,
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
Thorsten Albers
Joined: 04 Oct 2007 Posts: 756
|
Posted: Sat Jul 22, 2006 7:00 pm Post subject: Re: VB6 - Initializing a variable |
|
|
Andrew Chalk schrieb im Beitrag
...
> 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
---------------------------------------------------------------------- |
|
| Back to top |
|
 |
Jim Mack
Joined: 04 Oct 2007 Posts: 735
|
Posted: Sat Jul 22, 2006 11:08 pm Post subject: Re: VB6 - Initializing a variable |
|
|
Thorsten Albers wrote:
>
> 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
>
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 |
|
| Back to top |
|
 |
Thorsten Albers
Joined: 04 Oct 2007 Posts: 756
|
Posted: Sat Jul 22, 2006 8:36 pm Post subject: Re: VB6 - Initializing a variable |
|
|
Jim Mack schrieb im Beitrag
...
> 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).
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
---------------------------------------------------------------------- |
|
| Back to top |
|
 |
J French
Joined: 04 Oct 2007 Posts: 2725
|
Posted: Sun Jul 23, 2006 1:46 pm Post subject: Re: VB6 - Initializing a variable |
|
|
On Sat, 22 Jul 2006 16:37:16 -0500, "Andrew Chalk"
wrote:
>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 |
|
| Back to top |
|
 |
Bob O`Bob
Joined: 04 Oct 2007 Posts: 1456
|
Posted: Sun Jul 23, 2006 5:27 pm Post subject: Re: VB6 - Initializing a variable |
|
|
Andrew Chalk wrote:
> 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
-- |
|
| Back to top |
|
 |
Andrew Chalk
Joined: 04 Oct 2007 Posts: 15
|
Posted: Sun Jul 23, 2006 7:56 pm Post subject: Re: VB6 - Initializing a variable |
|
|
Thanks all for your help. It appears that I can assume that a UDT is
initialized to all NULs by VB.
- A
"Jim Mack" wrote in message @TK2MSFTNGP05.phx.gbl...
Thorsten Albers wrote:
>
> 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
>
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 |
|
| Back to top |
|
 |
Tony Proctor
Joined: 04 Oct 2007 Posts: 1051
|
Posted: Mon Jul 24, 2006 12:54 pm Post subject: Re: VB6 - Initializing a variable |
|
|
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
"Jim Mack" wrote in message@TK2MSFTNGP05.phx.gbl...
Thorsten Albers wrote:
>
> 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
>
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
|
|
| Back to top |
|
 |
|
|
| Related Topics: | Initializing an array of structs Hi all, is there a way to initialize a costant array of structs ? Do i need to initialize separately each members of each structure ? Thanks ! -- Stefano M
Object variable or With block variable not set error that sh I'm trying to encapsulate some common report functionality in my app in a report class module. Below is how I declare and instantiate the class. Note that there is no error when I display the report. Option Explicit Dim arCurrent As clsReport Private Sub
Object variable or with block variable not set I have an object variable defined in the module - something like Dim xxx as Object. In one of the class module say class A, i set the value of xxx, by doing something like Set xxx = objManager. In another class module say Class B, in one of the functions
pass variable Hi, Can I pass a variable from a form to another? Do I have to define again this var in another page? for example: I define a variable in MDIfrom and assign it a value, I want another form when loaded to get this value of the variable. Thanks for any idea
Reading a variable field I am trying to do the following: - update the field "page" of a pivot table with a variable that is from a cell in an Excel spreadsheet. - the value will vary depending on the value in the cell Set-up: user has a drop down list and select a "value", this |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|