Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object cloning

Posted on 2004-10-22
6
Medium Priority
?
251 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
Comment
Question by:bohiggy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:aacool
ID: 12384497
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
ID: 12385574
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
ID: 12386162
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:Farshid-Zaker
ID: 12386227
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
ID: 12395327
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:
Farshid-Zaker earned 2000 total points
ID: 12399654
Ok,
If your problem is solved please Accept the Answer!

<Farshid/>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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