473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to store Struct object to Build-in Collection?

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

I defined a module contained a custom Struct as below

Expand|Select|Wrap|Line Numbers
  1. Public Type Student
  2.    ID As Integer
  3.    Name As String
  4. End Type
I have another Form as GUI and uses the custom Struct.

Expand|Select|Wrap|Line Numbers
  1. Dim Std as Student
  2. Std.ID = 1
  3. Std.Name = "John"
  4.  
  5. Dim Coll as Collection
  6. Set Coll = New Collection
  7.  
  8. 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!

Expand|Select|Wrap|Line Numbers
  1. Public Type Student
  2.        ID As Integer
  3.        Name As String
  4.     End Type
Aug 21 '13 #1

✓ answered by ADezii

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.

7 2123
jimatqsi
1,271 Expert 1GB
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
Aug 21 '13 #2
zmbd
5,501 Expert Mod 4TB
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
Aug 21 '13 #3
ADezii
8,834 Expert 8TB
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.
Aug 21 '13 #4
jimatqsi
1,271 Expert 1GB
I'm learning a lot this week. :)
Aug 21 '13 #5
ADezii
8,834 Expert 8TB
Life is a learning experience for all of us, isn't it? (LOL).
Aug 21 '13 #6
Sakal
2
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,
Aug 22 '13 #7
zmbd
5,501 Expert Mod 4TB
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
Aug 22 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
3
by: RSB | last post by:
Hi Every one, Can i store a Object in the Session variable??? if Yes then HOW? Thanks RSB
2
by: jordanp | last post by:
Hello, I'm having a little trouble here and I'm hoping that somebody might be able to help me out (win32 console program). First off, I know that I can use class function inside of my struct as...
3
by: Ashutosh | last post by:
Hello, I am a newbie in CSharp. In C++ if we want to read from socket into the struct object we directly type cast it as recv(socketid,(char*),&struct_object,sizeof(struct object)) But C...
11
by: Jay Dee | last post by:
Object garbage collection of events I can not help wondering how the garbage collection system handles events. If I release all references to an object that has a void that an event somewhere...
1
by: Humakt | last post by:
I've seen the solution to do this vice versa, but what if I have an object that has collection and I want to return the objects from query using where clause where I check if any of the items in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.