Link to home
Start Free TrialLog in
Avatar of s_lavie
s_lavie

asked on

copy classes

Hi,
I want to ask how can I copy a content of one class to another (of the the same type), with no setting a reference.
what I mean is this:

dim a as new myClass
dim b as new myClass
b = a  (NOT set b = a)
Avatar of waty
waty
Flag of Belgium image

The best solution is adding a function to your class to copy the members : CopyOwnClass(clsDest as myClass)

So, you could do as follow :

   Sub CopyOwnClass(clsDest as myClass)

       With clsDest
           .A = A
           .B = B
       End With

   End Sub

Avatar of s_lavie
s_lavie

ASKER

Sorry waty, but this is the naive way to do so, it's not what I meant. What if you have a lots of vars in a class - would you do the same??? It's not realistic.
In C++ you just do a = b or *a = *b. Is there a similar way in VB?
Error, in C++ if you do a=b; you copy the adress. So if your class contains pointer, you could go to have some incoherences.

Under C++, you have to construct a copy constructor. In this copy constructor, you copy all the members as you need. It is the same under VB, but you have to call explicitely to the copy function.
Avatar of s_lavie

ASKER

It seems to me that there must be another way, otherwise I wouldn't ask that question.
If you know another way, I would like to know it too.

Drop a mail to Microsoft.
dim a as new myClass
dim b as myClass
set b = a

Avatar of s_lavie

ASKER

Hi hakyemez,
your answer setes a reference to of the class, but I want a completly different class with the same values, with out any connection, (like in numeric vars).
ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of s_lavie

ASKER

Sorry Waty,I probably misunderstood the way it works!