Results 1 to 8 of 8

Thread: VB Cannot return UDT From Class. y?

  1. #1

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Question 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
    Attached Images Attached Images  
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Last edited by LaVolpe; Jul 11th, 2009 at 08:47 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Question 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
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  4. #4
    Junior Member
    Join Date
    Feb 2008
    Posts
    23

    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

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  6. #6

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    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
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    Last edited by Milk; Jul 12th, 2009 at 11:55 AM. Reason: spellage

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB Cannot return UDT From Class. y?

    Quote Originally Posted by coolcurrent4u View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width