Object cloning

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.
bohiggyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aacoolCommented:
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
Farshid-ZakerCommented:
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
bohiggyAuthor Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Farshid-ZakerCommented:
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
bohiggyAuthor Commented:
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
Farshid-ZakerCommented:
Ok,
If your problem is solved please Accept the Answer!

<Farshid/>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.