使用指针什么类型都可以使用 type k end type dim j as k copymoney j,address,len(j) 其中 address 是你的类的参数的地址 varPtr()
给你一个例子,当然还有其它方法! Module1 ----------------------Option ExplicitPublic Type MyFriend sName As String bSex As Boolean iAge As Integer sAddress As String End Type Sub main() Dim tFriend As MyFriend
Load Form1
Dim oFriend As New cFriend oFriend.sName = tFriend.sName oFriend.bSex = tFriend.bSex oFriend.iAge = tFriend.iAge oFriend.sAddress = tFriend.sAddress Call Form1.TestVar(oFriend)End SubcFriend.Cls -------------------------------------- Option Explicit Private mvarsName As String 'local copy Private mvarbSex As Boolean 'local copy Private mvariAge As Integer 'local copy Private mvarsAddress As String 'local copy Public Property Let sAddress(ByVal vData As String) mvarsAddress = vData End Property Public Property Get sAddress() As String sAddress = mvarsAddress End PropertyPublic Property Let iAge(ByVal vData As Integer) mvariAge = vData End Property Public Property Get iAge() As Integer iAge = mvariAge End PropertyPublic Property Let bSex(ByVal vData As Boolean) mvarbSex = vData End Property Public Property Get bSex() As Boolean bSex = mvarbSex End PropertyPublic Property Let sName(ByVal vData As String) mvarsName = vData End Property Public Property Get sName() As String sName = mvarsName End PropertyForm1.frm ------------------------ Option ExplicitPublic Sub TestVar(cAmiker As cFriend)End Sub
从FORM 向 CLASS 传递 UDT 你应该把 UDT 结构 放入 模块中, 或 定义成 公共属性 Public ====================================================== 比如 UDT 为: 在模块中: ========================= Public Type MyUDT Param1 As String * 45 Param2 As String * 45 Param3 As String * 45 Param4 As Long Param5 As Long End Type
在窗体中: =========================
Option Explicit
Private xyz As MyUDT
Dim clsTest As New clsTest
Private Sub Command1_Click() With xyz .Param1 = "VB Developers " .Param2 = "Sonicdater" .Param3 = "http://www.163.org/" .Param4 = 1312 .Param5 = 18 End With clsTest.mEntryPoint VarPtr(xyz) End Sub 在 Class 中( Name: clsTest):
============================ 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 Dim T(1 To 5) CopyMemory ByVal abc, ByVal ptrType, ByVal LenB(abc)
With abc T(1) = .Param1 T(2) = .Param2 T(3) = .Param3 T(4) = .Param4 T(5) = .Param5 End With Debug,Print CStr(ptrType) End Sub
从 FORM1 向 FORM2 传递 UDT ================================================================= '模块部分: Public Type MyUDT Param1 As String * 45 Param2 As String * 45 Param3 As String * 45 Param4 As Long Param5 As Long End Type ====================================== 'FORM1 Option ExplicitPrivate xyz As MyUDT
Private Sub Command1_Click() With xyz .Param1 = "Developers" .Param2 = "Sonicdater" .Param3 = "TEST PASS UDT" .Param4 = 1312 .Param5 = 18 End With
Form2.Show Form2.mEntryPoint VarPtr(xyz)End Sub ======================================== 'Form2 'Add a label (Label1) and five text boxes (Text1 - Text5) to Form2, 'along with the following codeOption ExplicitPrivate 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
还有一个办法,
最好将Type改成类!!!!!
第二个方法能否给个例子?
public sub yourfunction (mytype as variant)
end sub
调用时
dim mytype as mytype
call yourfunction(mytype)
运行后出现错误如下 编译错误:
只有定义在公共对象模块中用户定义类型能和变体类型相互转换或传递给后期绑定功能。
type k end type
dim j as k
copymoney j,address,len(j)
其中 address 是你的类的参数的地址 varPtr()
Module1
----------------------Option ExplicitPublic Type MyFriend
sName As String
bSex As Boolean
iAge As Integer
sAddress As String
End Type
Sub main()
Dim tFriend As MyFriend
Load Form1
Dim oFriend As New cFriend
oFriend.sName = tFriend.sName
oFriend.bSex = tFriend.bSex
oFriend.iAge = tFriend.iAge
oFriend.sAddress = tFriend.sAddress
Call Form1.TestVar(oFriend)End SubcFriend.Cls
--------------------------------------
Option Explicit
Private mvarsName As String 'local copy
Private mvarbSex As Boolean 'local copy
Private mvariAge As Integer 'local copy
Private mvarsAddress As String 'local copy
Public Property Let sAddress(ByVal vData As String)
mvarsAddress = vData
End Property
Public Property Get sAddress() As String
sAddress = mvarsAddress
End PropertyPublic Property Let iAge(ByVal vData As Integer)
mvariAge = vData
End Property
Public Property Get iAge() As Integer
iAge = mvariAge
End PropertyPublic Property Let bSex(ByVal vData As Boolean)
mvarbSex = vData
End Property
Public Property Get bSex() As Boolean
bSex = mvarbSex
End PropertyPublic Property Let sName(ByVal vData As String)
mvarsName = vData
End Property
Public Property Get sName() As String
sName = mvarsName
End PropertyForm1.frm
------------------------
Option ExplicitPublic Sub TestVar(cAmiker As cFriend)End Sub
你应该把 UDT 结构 放入 模块中, 或 定义成 公共属性 Public
======================================================
比如 UDT 为:
在模块中:
=========================
Public Type MyUDT
Param1 As String * 45
Param2 As String * 45
Param3 As String * 45
Param4 As Long
Param5 As Long
End Type
在窗体中:
=========================
Option Explicit
Private xyz As MyUDT
Dim clsTest As New clsTest
Private Sub Command1_Click()
With xyz
.Param1 = "VB Developers "
.Param2 = "Sonicdater"
.Param3 = "http://www.163.org/"
.Param4 = 1312
.Param5 = 18
End With
clsTest.mEntryPoint VarPtr(xyz)
End Sub
在 Class 中( Name: clsTest):
============================
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
Dim T(1 To 5)
CopyMemory ByVal abc, ByVal ptrType, ByVal LenB(abc)
With abc
T(1) = .Param1
T(2) = .Param2
T(3) = .Param3
T(4) = .Param4
T(5) = .Param5
End With
Debug,Print CStr(ptrType)
End Sub
=================================================================
'模块部分:
Public Type MyUDT
Param1 As String * 45
Param2 As String * 45
Param3 As String * 45
Param4 As Long
Param5 As Long
End Type
======================================
'FORM1
Option ExplicitPrivate xyz As MyUDT
Private Sub Command1_Click() With xyz
.Param1 = "Developers"
.Param2 = "Sonicdater"
.Param3 = "TEST PASS UDT"
.Param4 = 1312
.Param5 = 18
End With
Form2.Show
Form2.mEntryPoint VarPtr(xyz)End Sub
========================================
'Form2
'Add a label (Label1) and five text boxes (Text1 - Text5) to Form2,
'along with the following codeOption ExplicitPrivate 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