• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Copying Objects in VB 5

Is there any way to copy an object that references a collection.  For example,  COLL is a collecion of Widgets.  I have a variable WIDGET1 that is a widget.  I want to update COLL(XX) with WIDGET1.  Now I am setting every property and it is very tedious:
COLL(XX).property1= WIDGET1.property1
COLL(XX).property2= WIDGET1.property2
  • 4
  • 3
  • 2
  • +1
1 Solution
You Are Doing it Wrong Here I have had this problem before so,

Dim CurrentWidget as Widget
Set CurrentWidget = Coll (XX)
CurrentWidget.Property1 = Widget1.Property1
CurrentWidget.Property2 = Widget1.Property2
You Have To Copy the Object From the Collection to the Type of the Object, in this case Widget. You Cannot Modify a Collection Directly. This Should Work OK.
MichaelJCAuthor Commented:
What I am doing now works okay.  What I would like to avoid is listing all of the collections properties to do this.  Some have 20-30 properties and it is very tedious listing all of them. I was looking for a way to just say these objects are of the same type so exchange them or something like that.
If I understand your question, correctly, you should be able to just do this:

Type Widget
  Prop1 as string
  Prop2 as integer
  PropX as date
End Type

Dim Wid1 as Widget
Dim Wid2 as Widget

With Wid1
End With

Now the copy:


I use this a lot. I define a blank, or default, Widget, and set newWidget = defaultWidget to initialize the type structure.

Hope this helps!

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

MichaelJCAuthor Commented:
That works when useing a user-defined type outside a collection, but I am using a collection of objects that are defined by a class module.  
Objects in collections are effectively ReadOnly.
You have to copy the object delete it from the collection and then add it back in.

dim COLL as Collection
dim myWidget as Widget

'To Change COLL(3)
set myWidget = COLL(3)
myWidget.MyProperty = newValue
COLL.Remove 3
COLL.Add myWidget,,,2 'This add the modified Widget after the 2nd item, ie as the third.

This is the biggest problem with collections.
MichaelJCAuthor Commented:
That's what I thought...

Not To be rude but you don't need to even remove the object after updating the property, the Dim statment creats an object reference ONLY so if you change one of the properties then all you have to is leave it at that.
How do you think when I have a Class Like this

Class Test
Public MyProperty as String
End Class

If i had the class in a collection all i would have to do is this
Dim X as Test
Set X=Col(X)
X.MyProperty = newValue
That is it
You don't have to change anything else.
Oh yeah the Objects them selves are read only BUT the properties on the object are read/write, how do you think you can change properties on the readonly Extender Object on a user control, it may be read only but as i said the properties are not...
Oops! AllenC_Jr, you're right on that one. Providing you add an object to the collection you're fine as you can access the properties etc of the object, so you can change them. Silly slip.
MichaelJCAuthor Commented:
Thanks ALLEN.  I think my problem was that I was using Dim myclass as new myclass.  In this instance, since I am creating a new instance, I think that may be why I had to move all the properties manually.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now