Link to home
Create AccountLog in
Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Avatar of GraemeScotland
GraemeScotland

Copying an object tree
Hello, I have the following object tree

clsRequest
|---strX
|---strY
|---strZ
|---Tests
    |---strA
    |---strB
|---ExtractVariables
    |---strC
    |---strD
|---FormData
    |---strE
    |---strF

In my program, I would like to be able to duplicate an existing Request object tree and create a new one which is independent from the first.

At the moment, I have been able to make an exact duplicate of the original using the following code

Dim objNewMember as clsRequest
Set objNewMember = objRequest

although this makes only a pointer to the original object tree. If a change is made to one, it affects the other too.

I have also tried making a new object tree from scratch and copying all the elements across one by one like so:

Dim objNewMember as clsRequest
Set objNewMember = New clsRequest

With objNewMember
   .strX = objRequest.strX
   .strY = objRequest.strY
   .Tests = objRequest.Tests
   .ExtractVariables = objRequest.ExtractVariables
   .FormData = objRequest.FormData
End With

This seems to work OK with the properties of the objNewMember object but the objects don't get copied across properly and so if a change is made in the Tests object of either original or new object then it will affect both.

Does anyone have any ideas how to do this?

Thanks in advance


Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of andyclapandyclap🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of GraemeScotlandGraemeScotland

ASKER

I am reading that I need a thing called a Deep Copy. Hopefully something will click into place soon :-)

Avatar of andyclapandyclap🇬🇧

Deep Copy is not a thing, it's a concept - so don't wait around for somebody to post a link to deepcopy.dll ;)
All deep copying means is that you copy the contents of objects rather than the reference to the object - just as you're doing above.

e.g.
'shallow copy a and b point to same object
set a=b

'full deep copy, b becomes a clone of a, and b's objmember becomes a clone of a's objmember
set b=new Class1
b.member1=a.member1
b.member2=a.member2
set b.objmember=new Class2
b.objmember.memberA=a.objmember.memberA

Avatar of AzraSoundAzraSound🇺🇸

Andy is correct.  You need to do it manually.  If you have a ton of variables/properties that you would rather not copy by hand, I may be able to find an old routine of mine to generate the "Clone" method for you.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Write a Dump() method for the object which puts its structure out as XML, either file based or string based.

Write a similar method to Fill() a new instance of an object, i.e. which takes an XML file and populates the object.

aye right!

Ok, i find the easy way...
in 30 minutes i will send

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


I have found the following way (no easy,use control array):
1)Change TreeViewCtrl properies Index from 'blank' to 0
2)Change all previous using of your TreeView (for example)
FROM    
set MyNode=MyTreeViewName.Nodes.Add("r1", tvwChild, "r3", "Mother")
TO
set MyNode=MyTreeViewName(0).Nodes.Add("r1", tvwChild, "r3", "State")
3)If you want to copy your treeview, make the following:

Load MyTreeViewName(1)
MyTreeViewName(1).Top = 1500  /* for example */
MyTreeViewName(1).Left = 1500 /* for example */
MyTreeViewName(1).Visible = True

P.S.:
Apreare some problem - open all lists (it's necessary find a properties for blocking it.)

Seems fine, if I was talking about TreeView controls...

I was meaning an object which consists of pointers to other objects and member variables.

Sorry

Avatar of DanRollinsDanRollins🇺🇸

Hi GraemeScotland,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept andyclap's comment(s) as an answer.

GraemeScotland, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.