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?
 
Farshid-ZakerConnect With a Mentor Commented:
Ok,
If your problem is solved please Accept the Answer!

<Farshid/>
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
All Courses

From novice to tech pro — start learning today.