Assign a clone

Posts   
 
    
Posts: 256
Joined: 05-Jul-2010
# Posted on: 07-Jul-2010 15:19:15   

Hi

I would like to copy object B into object A. It needs to copy all the fields, except the PK fields, into another instance of the same class.

Like the function below. Would there be any faster / better method to do this?

Thanks

Alexander

public void Assign(Object aObject) {
  if (aObject.GetType() != GetType()) {
    throw new ApplicationException("Cannot assign to a different class");
  }
  if (!Type.ReferenceEquals(this, aObject)){
    for (int i = 0; i < Fields.Count; i++){
      this.SetValue(i, aObject.GetValue(i, false));
    }
  }
}
Walaa avatar
Walaa
Support Team
Posts: 14951
Joined: 21-Aug-2005
# Posted on: 07-Jul-2010 15:31:51   

Your code seems fine for copying data.

Posts: 256
Joined: 05-Jul-2010
# Posted on: 08-Jul-2010 10:47:16   

Walaa wrote:

Your code seems fine for copying data.

Ok, Thanks