Solved

Copying Objects in VB 5

Posted on 1998-09-22
10
143 Views
Last Modified: 2013-11-23
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
etc.
0
Comment
Question by:MichaelJC
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Expert Comment

by:AllenC_Jr
ID: 1436116
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
Etc.
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.
0
 

Author Comment

by:MichaelJC
ID: 1436117
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.
0
 
LVL 3

Expert Comment

by:kfrick
ID: 1436118
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
  .Prop1="Widget"
  .Prop2="0"
  .PropX=Now()
End With

Now the copy:

Wid2=Wid1

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

Hope this helps!

-kf
0
 

Author Comment

by:MichaelJC
ID: 1436119
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.  
0
 

Accepted Solution

by:
sjrl earned 10 total points
ID: 1436120
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.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:MichaelJC
ID: 1436121
That's what I thought...


0
 
LVL 2

Expert Comment

by:AllenC_Jr
ID: 1436122
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.
0
 
LVL 2

Expert Comment

by:AllenC_Jr
ID: 1436123
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...
0
 

Expert Comment

by:sjrl
ID: 1436124
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.
0
 

Author Comment

by:MichaelJC
ID: 1436125
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.  
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now