|
|
![]() |
|
||
|
|
|||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
| Visual Basic Helper Routines CopyMemory: How to Copy a Private UDT Between Forms |
||
| Posted: | Friday June 01, 2001 | |
| Updated: | Monday December 26, 2011 | |
| Applies to: | VB4-32, VB5, VB6 | |
| Developed with: | VB6, Windows 2000 | |
| OS restrictions: | None | |
| Author: | VBnet, Randy Birch | |
| Prerequisites | |
| None. | |
|
|
|
Scenario 1: You have a user-defined type defined as public in a BAS module. In a form, you declare a UDT as Private at form level and use its members within the form. You add an additional form and want to pass the UDT to the second form, without making the UDT public to the entire application. Scenario 2: You have a user-defined type defined as private in a form. You declare the UDT as Private at form level and use its members within the form. You add an additional form and want to pass the UDT to the second form, without making the UDT public to the entire application. User-defined types can not be passed from one form to another by simply defining a method in the second from, and then defining a parameter within that method As UDT. For example, defining a sub or function as pubic in a form, then defining the method's parameter list using syntax such as (abc as MyUDT) will generate an error on compile. When a UDT needs to be passed to a method in another form you can do so by defining the receiving method's parameter as Long instead of as the UDT type, passing VarPtr(MyType) as the calling parameter, and, using CopyMemory, recopy the UDT data at the address pointer back into a UTD structure. For scenario 1 above, the only code needed in the second form is the CopyMemory declare, and the receiving sub. For scenario 2, the same is required, plus a copy of the UDT definition defined in the receiving form. The demo below shows the scenario 1 solution. |
|
| BAS Module Code | |
|
|
|
| Place the following code into the general declarations area of a bas module: | |
|
|
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Type MyUDT Param1 As String * 45 Param2 As String * 45 Param3 As String * 45 Param4 As Long Param5 As Long End Type |
|
| Form1 Code | |
|
|
|
| Toss a command button onto Form1 along with the following code: | |
|
|
|
Option Explicit
Private xyz As MyUDT
Private Sub Command1_Click()
With xyz
.Param1 = "VBnet Developers Resource Centre"
.Param2 = "Randy Birch"
.Param3 = "http://vbnet.mvps.org/"
.Param4 = 1312
.Param5 = 18
End With
Form2.Show
Form2.mEntryPoint VarPtr(xyz)
End Sub |
|
| Form2 Code | |
|
|
|
| Add a label (Label1) and five text boxes (Text1 - Text5) to Form2, along with the following code: | |
|
|
|
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDst As Any, _
pSrc As Any, _
ByVal ByteLen As Long)
Public Sub mEntryPoint(ptrType As Long)
Dim abc As MyUDT
CopyMemory ByVal abc, ByVal ptrType, ByVal LenB(abc)
With abc
Text1.Text = .Param1
Text2.Text = .Param2
Text3.Text = .Param3
Text4.Text = .Param4
Text5.Text = .Param5
End With
Label1.Caption = CStr(ptrType)
End Sub |
|
| Comments | |
|
|
|
|
|
|||||
|
|||||
|
|
|||||
|
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |
![]() |