473,465 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to add an object to a collection?

sueb
379 Contributor
I have a collection manager (managing instances of forms) that was working fine, but I broke it and, since it was developed by someone else, I don't understand it well, so I don't know how to fix it.

The problem is that when I try to open another instance of a form, I get the runtime error 457, saying "This key is already associated with an element of this collection" on line 27 of the following code:

Expand|Select|Wrap|Line Numbers
  1. Function OpenAClient(frmForm As Form, _
  2.                      frmCaption As String, _
  3.                      frmCollection As Collection, _
  4.                      Optional strFilter As String = "")
  5.     ' Purpose: Open a new instance of the specified form, manage its properties, and append it
  6.     ' to the specified collection
  7.  
  8.     ' Open a new instance and set a caption
  9.     frmForm.Caption = frmCaption & " " & frmCollection.Count & ", opened " & Now()
  10.  
  11.     ' Set the form's editing mode according to the current user's privileges
  12.     If ReadWrite Then
  13.         frmForm.RecordsetType = conDynaset
  14.     Else
  15.         frmForm.RecordsetType = conSnapshot
  16.     End If
  17.  
  18.     ' Use a pointer, if one was supplied
  19.     If strFilter & "" <> "" Then
  20.         frmForm.Filter = strFilter
  21.         frmForm.FilterOn = True
  22.     Else
  23.         frmForm.FilterOn = False
  24.     End If
  25.  
  26.     ' Append it to the specified collection and display it
  27.     frmCollection.Add Item:=frmForm, Key:=CStr(frmForm.Hwnd)
  28.     frmForm.Visible = True
  29.  
  30. End Function
  31.  
I originally had just one collection of forms, but later needed this same functionality for other forms, so I made it a common routine that accepts the collection to handle (instead of hard-coding the collection within it). That's what broke it, I think. I suspect that the property .Hwnd (what does that stand for, anyway?) is not getting incremented; in fact, I suspect that the collection is not getting returned out of this function. I don't know how to specify an "in out" parameter, but I'm thinking that's what needs to happen?

Here's an example of how I call it:

Expand|Select|Wrap|Line Numbers
  1. Function OpenAnOverviewClient(Optional strFilter As String = "")
  2.     OpenAClient Form_Patient_IUR_Overview, "PAT", clnOverviewClient, strFilter
  3. End Function
  4.  
Mar 10 '11 #1

✓ answered by ADezii

@sueb, I'm really pressed for time, but I threw together something for you to look at, that should be workable.

9 4130
NeoPa
32,556 Recognized Expert Moderator MVP
Another question of quite enormous scope again I'm afraid Sue.

What I can tell you is that to pass a parameter to a procedure which can be used by the procedure as well as updated (such that the calling code has the updated state of the parameter when it's finished) you use the ByRef keyword. Here's an illustration :

Expand|Select|Wrap|Line Numbers
  1. Private Sub TestByRef(ByRef intX As Integer)
  2.     intX = intX + 10
  3. End Sub
The following code illustrates this working :
Expand|Select|Wrap|Line Numbers
  1. Dim intA As Integer
  2.  
  3. intA = 25
  4. Call TestByRef(intA)
  5. Debug.Print intA
The value printed in the Immediate Pane would be 35. Not 25 as you would expect without ByRef being specified.
Mar 11 '11 #2
sueb
379 Contributor
I added "ByRef" to the parameter frmCollection, but still got the same error. What is happening with that .Hwnd property anyway? What is it?
Mar 11 '11 #3
ADezii
8,834 Recognized Expert Expert
The problem is that your Function never actually creates a New Instance of the Form passed to it as an Argument. This is only accomplished by using the New Keyword, as in:
Expand|Select|Wrap|Line Numbers
  1. Dim frm As Form_frmCustomers
  2. Set frm = New Form_frmCustomers
  3.  
  4. 'Assuming your Collection has already been Defined
  5. frmForm.Caption = "Instance Number " & (frmCollection.Count + 1) & ", opened " & Now()
  6.  
  7. ' Append it to the specified collection and display it
  8. frmCollection.Add Item:=frm, Key:=CStr(frm.hWnd)
  9. frmForm.Visible = True
P.S. - The hWnd Property of a Form is a Unique Long Integer Value, assigned by Microsoft Windows to the Current Window (in this case each Form Instance). Since the 'Key' Component of a Collection must be a Unique Value for each Element, hWnd is a Logical choice.
Mar 11 '11 #4
sueb
379 Contributor
But how do I pass in to the function the different forms that I want it to manage? I have four different form collections that I want to manage in the same way, without having to duplicate code. In the example, the Dim statement is serving a single form type.
Mar 11 '11 #5
ADezii
8,834 Recognized Expert Expert
@sueb, I'm really pressed for time, but I threw together something for you to look at, that should be workable.
Attached Files
File Type: zip Multiple Instances.zip (16.2 KB, 104 views)
Mar 11 '11 #6
sueb
379 Contributor
@ADezii, thanks a bunch--I'll look at it as soon as I get a chance!
Mar 11 '11 #7
sueb
379 Contributor
@ADezii, this looks like it may have done the trick! So, basically, the change is to add, to each calling routine, a Dim and a Set statement to create the object that I'm sending in to the collection manager.

Thanks--I hadn't really understood the scope of the object correctly, but this makes it clearer.
Mar 11 '11 #8
sueb
379 Contributor
@NeoPa, thanks for the explanation of the meaning and use of the .Hwnd property--that helps my basic understanding of the objects being managed.
Mar 11 '11 #9
NeoPa
32,556 Recognized Expert Moderator MVP
I'm glad things seem to be sorted out now.

I think my point was about the ByRef actually, but that's just being precise ;-)

I think you've already got what you need from ADezii, but collections can be managed within a procedure if one of the arguments to the procedure is a collection object. In such cases the ByRef effect is automatic anyway of course.
Mar 14 '11 #10

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

Similar topics

4
by: m. pollack. | last post by:
Hi all, Is there any information to be had about the "Object Collection Editor" that appears when you click on a collection property in the PropertyGrid control? I have a class that maintains a...
0
by: ron | last post by:
Hi, I currently have a jagged array as such. int myJArray = new ; In my constructor i initialize the array of arrays like such. for(int i=0; i<21; i++) { myJArray = new int;
9
by: Alfred Taylor | last post by:
I'm testing the waters of n-tier development and I ran into a scenario that I'm not sure what the best solution would be. I have a Company object which contains a collection of contacts retrieved...
1
by: Weston Weems | last post by:
I've got a collection of objects say fruit. and fruit has a fairly decent heirachial structure not just flat props. I've got my repeater to bind to my fruit collection just fine, but what I want...
3
by: Nak | last post by:
Hi there, I am trying to make a collection that will work in the "Object collection editor" dialog. I have implimeneted ICollection, IList and IEnumerable and now I have an "unnamed"...
0
by: ECathell | last post by:
I have an object called box. It has the following properties BoxType BoxTare BoxDescription it is in a Namespace of x.database.Boxes Box is a row in my database, but in my UI I will be...
0
by: a | last post by:
Q. Unable to get a Profile custom (object) collection to bind to GridView,etc (IList objects)? This is my first custom object so I may be doing something rather simple, wrong, or it may be...
0
by: Jon | last post by:
Hello everyone. If I have a collection, that contains objects of a different type, but all inherit from the same base class, and this base class has an interface on it, can I bind the collection...
2
by: Torsten Z | last post by:
Hi, I have tried several things to passing an array of objects or a Object Collection from .NET to VB6 but I can't get it work. Passing one object works fine, but more than one not. Has anybody...
3
by: gasfusion | last post by:
Hey guys. I'm building an object collection which will be a part of a Data Access Layer i am currently working on. However, i am having some issues iterating through a collection. This is what...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.