Solved

Copying Objects in VB 5

Posted on 1998-09-22
10
144 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 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

8 Experts available now in Live!

Get 1:1 Help Now