How to store Struct object to Build-in Collection?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sakal
    New Member
    • Aug 2013
    • 2

    How to store Struct object to Build-in Collection?

    I've try to store the Struct object to Collection, but i failed.

    I defined a module contained a custom Struct as below

    Code:
    Public Type Student
       ID As Integer
       Name As String
    End Type
    I have another Form as GUI and uses the custom Struct.

    Code:
    Dim Std as Student
    Std.ID = 1
    Std.Name = "John"
    
    Dim Coll as Collection
    Set Coll = New Collection
    
    Coll.Add Std, Std.ID
    I got the error message as below.

    only user-defined types difined in public object modules can be coerced to or from a variant or passed to late-bound functions


    Please help me out!

    Code:
    Public Type Student
           ID As Integer
           Name As String
        End Type
    Last edited by zmbd; Aug 21 '13, 11:38 AM. Reason: [rabbit{[please use code tags}][z{related code in post#2 to op}{placed code tags for post#2 block}]
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1288

    #2
    Sakal, what Rabbit is telling you about code tags means in order to post code, click on the "[CODE/]" button and then type or paste your code between the beginning and ending code tags that appear.

    Now, as to your problem, we need to see the entire code module for your first bit of code. I've tried just what you have here and I don't have any problem.

    Jim

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      Sakal
      Before you post the code please follow all of the trouble shooting steps outlined here> > Before Posting (VBA or SQL) Code

      Following the troubleshooting steps as outlined will fix a lot of basic issues.

      In this case, you more than likely have a mis-classed variable. The debug/compile will should help you to find the issue.

      Once you can compile without errors, then run the code. If you are still haveing the same issue then please post back.

      thnx

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        The answer is actually quite simple, you cannot use a Collection to contain UDTs (User Defined Types). You can, however, have a Collection of Classes, so convert the Type to a Class.

        Comment

        • jimatqsi
          Moderator Top Contributor
          • Oct 2006
          • 1288

          #5
          I'm learning a lot this week. :)

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Life is a learning experience for all of us, isn't it? (LOL).

            Comment

            • Sakal
              New Member
              • Aug 2013
              • 2

              #7
              I'm sorry for this inconvenience of my post.

              I've tried the solution of ADezii and it worked. Collection can't store UDTs, but instance object of class instead.

              Thanks,

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                Sakal
                I'm sorry for this inconvenience of my post
                Absolutely no reason to apologize! You have no idea how many people you and ADezii just helped!!!
                Would you please select the "best answer?"
                thnx

                Comment

                Working...