C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Making your class clonable.


Posted Date: 18 Jan 2007    Resource Type: Articles    Category: .NET Framework
Author: Prakash B BuddhabhattiMember Level: Bronze    
Rating: 1 out of 5Points: 7



Making your class clonabble



Objects are reference type. Actually objects won’t have any name. What we are creating with class name are the reference variables. Objects will be created when you use new keyword with class name. And the variables which we create just store the reference of the object. So when you make assignment operation between two reference variables just reference is stored in other variable here no separate memory allocation is done for the other reference variable. So now the situation is that that there are two reference variable which point to the same location in memory and any change made by any one reference variable will effect to the other. Now if you want that actually separate location should be created in memory and all the members should be copied to the new location then there is a clone method available. If you are using any system class or any third party class then if they provided the facility for cloning then you can just call the clone method and get the separate block for your object with same value. But if you want to provide cloning facility in your own class then you
have to implement your class from IClonable interface and you have to override the Clone method. In this method you have to write code for creating new object and copying all the members of existing object to the new one and then return the new object


  
Using System;
Class MyClass:IClonable
{
Int no1;
String str1;
public System.Object Clone()
{
MyClass objReturn=new MyClass();
objReturn.no1=no1;
objReturn.str1=str1;
return objReturn;
}
public static void Main(string [] args)
{
MyClass obj1=new MyClass();
obj1.no1=5;
obj1.str1="Hello";
MyClass obj2=obj1.Clone();
}

}






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
ICloneable  .  Class  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Project for Beginers: File Handling Utility
Previous Resource: Serialization
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use