1 Attachment(s)
VB Cannot return UDT From Class. y?
Hello guys am having trouble returning udt from a class
see what i did below
i put this in a module
Code:
'// UDT TICKETS //
Public Type udtTicket
Ticket_id As String
Ticket_pass As String
sCost As Integer
Time As String
Validity As Integer
FromTime As String
ToTime As String
Created As Date
User As String
End Type
i put this in this code in class
Code:
'###################################
'# Retrive a ticket #
'###################################
Public Function GetTicket(id As Integer) As udtTicket
GetTicket = sAllTicket(id)
End Function
guys pls help me out
Re: VB Cannot return UDT From Class. y?
The error is misleading. When talking about UDTs, UDTs could be passed from a compiled OCX, DLL but not from a module/class within your project. Just the way it is.
There are a few workarounds
1. Don't use a UDT, use a class where each class property is a UDT member, i.e., the class would have propeties of Ticket, Time, Validity, etc. Classes can be passed
2. It is possible to use CopyMemory to pass a UDT, however, since your UDT contains strings, a simple CopyMemory command really won't work. The workaround would be pretty much the same effort as #1 above.
3. The easiest method I think.
a. Declare a Public udt variable in your module, i.e., Public cachedUDT As udtTicket
:: this way you can access the public variable from anywhere in your project
b. In any function that should return a UDT, set the cachedUDT equal to what would be returned, i.e.
Code:
Public Sub GetTicket(id As Integer)
cachedUDT = sAllTicket(id)
End Function
c. In any function that should receive a UDT, set the cachedUDT also
Code:
... from within your form
GetTicket 123
cachedUDT.User = "LaVolpe"
UpdateTicket 123
' from within your class/module; wherever GetTicket method is...
Private Sub UpdateTicket(id As Integer)
sAllTicket(id) = cachedUDT
End Sub
Note: I see you are using strings for Date and Time members of the UDT. Recommend against that, recommend using either Date variable types or Double.
Edited & FYI: The error does won't occur if the methods were private vs public, but of course, being private the scope of the routines would be limited to that specific class/module.
Re: VB Cannot return UDT From Class. y?
Thanks for the quick respons
i actually have this code in a class, the class is in a module
and the whole this is in an activex dll. so i create an object of this class in my form, and call the form properties.
i'll first try to use a class ticket, but the problem is i already have a class name ticket.
its the class that actually host the module and class i mentiond earlier
Let me try the class thing
Re: VB Cannot return UDT From Class. y?
I'm pretty sure you need to use the "friend" scope.
iirc, this will work;
Code:
Friend Function GetTicket(id As Integer) As udtTicket
GetTicket = sAllTicket(id)
End Function
failing that, this should work;
Code:
Friend Sub GetTicket(id As Integer, Out as udtTicket)
Out = sAllTicket(id)
End Function
Re: VB Cannot return UDT From Class. y?
If you declare a class sub/function/property as Friend (rather than Public/Private) then it can pass UDT's declared public in the project to other members of the project. Unlike Public procedures, Friend procedures are not visible to the controllers of a project.
Edit: I see I've been beaten to it.
Re: VB Cannot return UDT From Class. y?
i declared the UDT in a module in the active x dll and still wont work. when i run, i get a user define type not defined for this
Code:
Friend Function GetTicket(id As Integer) As udtTicket
GetTicket = sAllTicket(id)
End Function
Re: VB Cannot return UDT From Class. y?
I've just tested it in an activeX DLL project and it works fine for me.
Remember it cannot be used to pass a UDT between the DLL and its controller. For that problem see Lavolpe's reply.
Re: VB Cannot return UDT From Class. y?
Quote:
Originally Posted by
coolcurrent4u
i declared the UDT in a module in the active x dll and still wont work. when i run, i get a user define type not defined for this
Code:
Friend Function GetTicket(id As Integer) As udtTicket
GetTicket = sAllTicket(id)
End Function
You may have one issue or two.
1. If the UDT is to be passed to other modules/classes within your DLL from the DLL, then using Friend function or the public varaible workaround I described should do the trick.
2. If you want to pass the UDT from the DLL to the project hosting your DLL and vice versa, then I believe you can do that via a Public function but, the UDT must be declared Public in the active-x class vs the module. Worth a try.