View Full Version : with new power UDTs, pass byref or byval
Eros, with the new power we have with User Defined Types and being able to nest them.
Should we still pass byref UDT's or can we use byval now. If both are possible, which is still preferred and way you would recommend?
Thanks.
ErosOlmi
15-12-2007, 18:57
Kent,
almost every programming language able to deal with UDTs (user defined types), when used as parameter function, pass them BYREF. BYREF means that just a pointer to the referenced data is passed into the stack allowing great efficiency and speed. A pointer is nothing more than a 32bit memory address (DWORD) on 32bit OS while a structure can be from one single byte to many thousands.
If you pass a structure BYVAL it means you make a full copy of the structure copying from one location to the other the full sequence of byte the structure is made: again from one single byte to many thousands.
Copying memory is one of the worst way to handle data because it takes time, it vaste memory, it creates unnecessary replicates of data, ... and many othe reasons. The correct way is to handle data inside UDT using pointers and function parameters passed BYREF are just pointers to real allocated data.
That said, there can be situations where making a full copy of some memory areas is needed in order to have local copies of structures to be able to change them without effecting some other referenced structures. In those cases you just need to create a local variable of that type and use PEEK/POKE functions or any other memory functions (API included) to copy structure data from a reference source to a local var.
Let me know if I replied to your question or you have specific cases where BYVAL of UDT is needed. I will evaluate them and consider to add BYVAL possibility also to UDTs.
Ciao
Eros
Thanks for the explanation Eros. Byref works out perfectly and I am glad it is the right way to go.