Solved

Object cloning

Posted on 2004-10-22
203 Views
Last Modified: 2010-04-15
Here's the problem:

I have a member function void fetch(int ID);
Inside this function I'm using a O/R mapping tool that fetches an object by id like this:

query = query.find("id").equals(ID);
someObject =(someObject) persistant.GetObjectFromDB(query);

and I'd like to continue by saying
this = someObject;

but I can't because the 'this' reference is read-only.  I could do this,

id = someObject.id
description = someObject.description ... and so forth, but this defeats the purpose of using the O/R mapping tool.

Does anyone have a slick solution requiring only a few lines of code?

Please note that changing the return type of fetch is not acceptable. It must not return anything. It only purpose is to fill an instance member with values from the databse.
0
Question by:bohiggy
    6 Comments
     
    LVL 3

    Expert Comment

    by:aacool
    Some unclear stuff

    a) You mention  void fetch(int ID), but it's not used in the code shown.

    b) what is the type of 'this'? What is the class derived from? Is there any relationshop to someObject?

    c) Why do you need to say    this = someObject? Why can you not continue to use someObject in your code?
    0
     
    LVL 4

    Expert Comment

    by:Farshid-Zaker
    If someObject class, is staticaly defined, implement a CopyTo method for it and call it in your fetch method:

    void CopyTo(someObject)
    {
       this.Id = someObject.Id;
       ...
    }

    But, if its type is variable, then you should use more complicated methods.
    Please let's know the exact situation.

    <Farshid/>
    0
     

    Author Comment

    by:bohiggy
    Sorry for being unclear. Let me clarify.

    Basically I want to be able to implement something like this w/ a non-static class

    void public CloneObject(Someobject someObject)
    {
       this = someObject;// error: <this> is read-only
    }

    where someObject and the <this> reference are of the same type.
    I don't want to go through each data member and copy values. I need a slick solution.

    The reason for this odd question is deeply rooted in the framework I'm using to build classes and the data access mechanism it employs.

    Thanks
    0
     
    LVL 4

    Expert Comment

    by:Farshid-Zaker
    ok,
    If you want to clone public attributes(fields) of someObject class use following code:

    public void CloneObject(Someobject someObject)
    {
          System.Type t = this.GetType();
          System.Reflection.FieldInfo[] fieldArray = t.GetFields();
          foreach (System.Reflection.FieldInfo field in fieldArray)
          {
                field.SetValue(this, field.GetValue(someObject));
          }
          //this = someObject;// error: <this> is read-only
    }

    But, if you want to copy properties of someObject use this code. That's obvious that, you can write a CloneObject method that does both jobs for you:

    public void CloneObject(Someobject someObject)
    {
          System.Type t = this.GetType();
          System.Reflection.PropertyInfo[] propArray = t.GetProperties();
          foreach (System.Reflection.PropertyInfo prop in propArray)
          {
                prop.SetValue(this, prop.GetValue(someObject));
          }
          //this = someObject;// error: <this> is read-only
    }

    //Attention: using this methods, you can not copy private attributes of your classes.
    <Farshid/>
    0
     

    Author Comment

    by:bohiggy
    Thanks for your help. In case anyone was wondering, if you implenent this method

    public void CloneObject(Someobject someObject)
                {
                      System.Type t = this.GetType();
                      System.Reflection.FieldInfo[] fieldArray = t.GetField(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Public);
                      foreach (System.Reflection.FieldInfo field in fieldArray)
                      {
                            field.SetValue(this, field.GetValue(someObject));
                      }
                      //this = someObject;// error: <this> is read-only
                }

    you can copy all private,public instance attributes of your classes.
    0
     
    LVL 4

    Accepted Solution

    by:
    Ok,
    If your problem is solved please Accept the Answer!

    <Farshid/>
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    931 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now