Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a User-defined-type

Status
Not open for further replies.

ToddR

Programmer
Feb 28, 2001
35
US
I have tried passing a udt between classes with no luck. Defining a public type in a standard module seemed logical, but trying to pass this creates a compile error. Defining it in as public in a class module also results in a compile error.

Here's how to reproduce the error: Start a new standard exe project, add a standard module and a class module. Add the following to the standard module:
Public Type struct
x As Integer
y As String
End Type

In the class module, add:
Public Sub TryIt(s As struct)
MsgBox "Success"
End Sub

Try to run it. If you know (or come up with) a workaround, please let me know. Thanks!

 
Hi,

Why don't you create a class using the two variables in your struct as private variables in the class? Like:

'definition of Class MyUDT
dim x As Integer
dim y As String


You can then declare an object of this class and pass the object like any other object to your other objects of different classes.

Have a good one!
BK
 
Thank you. That's what I ended up doing.
 
hey toddr

i feel your pain. my question was about using udt's as parameters and as return values of (class) functions i was
browsing this site to see if anybody else had the same problem.

two different references told me two different things. neither worked, nor did any twisted permutation i could think of work.

yes, i can pass objects this way with no problem. it just seemed wastful to me to make them objects when they were just a cluster of variables.

 
Are you getting an error to the effect "...must be a public creatable class.." or something like that?
This might work: define a public udt in an ActiveX DLL (instancing set to Multiuse). Then dim a private member variable as your udt type.

'''in the DLL
Public Type as PersonUDT
Name as String
Age as Integer
End Type
Private m_Person as PersonUDT


Public Sub FillUDT ()
m_Person.Name = "John"
m_Person.Age = 12
end sub


Property Get Person () as Person
Person = m_Person
end Property


'''To test this you will probably have to add this class to a Group project. Then add a standard exe project to the Group and reference the dll.

'''in standard exe
dim obj as Class.classname
Sub Form_OnLoad ()
dim NewPerson as Person
set obj = new Class.classname
obj.FillUDT
NewPerson = obj.Person
debug.Print NewPerson.Name
debug.Print NewPerson.Age
end sub

'''drawback is that your UDT in the DLL is PUBLIC!!
Let me know if it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top