PDA

View Full Version : How to clone a class?



HenryPotter
03-26-2009, 12:15 PM
Hi All,

I have defined a class called Market and have created an instance (Market1) to store the element inside the class.

Now I want to create an independent instance (Market2) that has the same value as my first instance but would not affect the value of the first instance.

I need to do that because I need to slightly change my second instance but not all elements inside.

I tried using Set Market2 = Market1 but this would link the elements together.

Does anybody have an idea how to achieve that?

Thanks a lot!

Henry

Kenneth Hobs
03-26-2009, 12:45 PM
Set Market2 = NEW Market

HenryPotter
03-26-2009, 01:09 PM
Thanks but it doesn't work. It says Userdefined type not defined.

Kenneth Hobs
03-26-2009, 01:10 PM
Just use New and use the same Class.

HenryPotter
03-26-2009, 01:14 PM
This is what I write:
Dim Market1 as Market
Set Market1 = New Market
Market1.S = 100
Market1.T = 2
Market1.D = 5

Dim Market2 as Market
Set Market2 = Market1
Market2.S=200

Now Market1.S = 200 to. So how can I make them independent?

Kenneth Hobs
03-26-2009, 01:21 PM
Dim Market2 As Market
Set Market2 = NEW Market
Market2.S=200

HenryPotter
03-26-2009, 01:25 PM
What if I want to have the other value of Market 1 ?

T = 2
D = 5

Do I have to key in code manually?

Kenneth Hobs
03-26-2009, 01:54 PM
Put your data in a Public variant and get the parts that you need for each. You can loops or create a Sub in your class to do it or another Sub.
Example doing it manually:
Public a as Variant

Dim Market1 As Market
Set Market1 = New Market
a = Array(100, 2, 5)
Market1.S = a(0)
Market1.T = a(1)
Market1.D = a(2)

mikerickson
03-27-2009, 08:39 PM
You might consider adding another method to the class. Sub BecomeTheSameAs(otherMarket as Market)