Click here to Skip to main content
15,112,662 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

How to copy all properties of a class to another class using byval method?
VB
Dim Xglobal as new NameClass
Dim Xlocal as new NameClass


XLocal = XGlobal


i use the above method, it works good, but when i change a property value in xglobal class it automatically changes the values of xlocal properties also. (I want to assign the values like byval methoid).
Posted
v2

Quote:
XLocal = XGlobal
The above line simply assigns the reference XGlobal to XLocal: they are then two variables holding the reference to the same object.

You should instead use MemberwiseClone[^] method. However, please, please, please, make sure to understand what a shallow copy is (and what a deep copy is), that is: read carefully the documentation (and the code example).
   
Comments
Maciej Los 17-Apr-13 3:33am
   
I forgot about that method ;(
+5!!!
CPallini 17-Apr-13 3:39am
   
Thank you.
You need to provide Copy() method ;) which returns new class with default properties.

in pseudo-code:
VB
Class NameClass

    'declare and implement many properties and methods

    'provide Copy method
    Public Function Copy() As NameClass
        Dim newClass As NameClass
        newClass.Property1 = Me.Property1
        newClass.Property2 = Me.Property2
        Return New newClass
    End Function

    'another members of class

End Class


Usage:
VB
Dim Xglobal as new NameClass
With Xglobal
   'here set properties
End With

Dim Xlocal as new NameClass = Xglobal.Copy() 'simple?
   
v2
Comments
kgmmurugesh 18-Apr-13 2:34am
   
Both Classes have the same property. I want to copy all the properties in a single line of code.
Maciej Los 18-Apr-13 2:44am
   
Don't you understand that Copy method will be in single line when you provide this method inside your class.
See my answer after update.
kgmmurugesh 20-Apr-13 1:57am
   
you copy individual properties by using class. I want to copy all properties in single line (don't want to copy individual properties)
Member-wise clone
create a new public function clone in your NameClass
VB
Public class NameClass
   Public Function clone() As NameClass
        Dim ObjOriginal = Me
        Dim ObjCopied As New NameClass
        'set desired property values from original object to the object you want to copy
        ObjCopied.Property1 = ObjOriginal.Property1
        Return ObjCopied 
    End Function
'other property & methods...
End Class

Now... instead of your code...
VB
Dim Xglobal as new NameClass
Dim Xlocal as new NameClass
XLocal = XGlobal.Clone()

Happy Coding!
:)
   
v2
Comments
kgmmurugesh 18-Apr-13 2:34am
   
Both Classes have the same property. I want to copy all the properties in a single line of code.
Aarti Meswania 18-Apr-13 2:37am
   
In this case,
It's only one way to achieve requirement and it is Memberwise-clone so, you have to write this function that set all properties
kgmmurugesh 18-Apr-13 2:41am
   
I can't understand memberwise clone, can you give me an example
Aarti Meswania 18-Apr-13 2:48am
   
all properties,methods,enum,type what ever declared in class... are called members of that class.

so here all properties in your 'NameClass' are members of it...

so, see function clone() in NameClass

I have write comment
"set desired property values from original object to the object you want to copy"
means set properties manually from current object to newly created object

in this process we are copying property(member) values from current object to new object. so this is called member(here property) wise cloning(copying).

let me know if you have doubts now :)
kgmmurugesh 20-Apr-13 1:51am
   
ok, But i want to copy all the properties in single line of code.
Aarti Meswania 20-Apr-13 1:53am
   
using "reflation" concept you can set properties... but still I will advice to list it manually :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900