Btw:
what to restructure your constructor to something like this and eliminate the need for cloning at all?
Creating 2nd instance of my own class in constructor is somewhat bizarre anyway.
Main Topics
Browse All TopicsHi,
I have created a business object that loads automatically from the database.
Can I simply assign a temporary object to 'this' in a constructor (this is a 101 question, I realise - ouch!).
If not, can someone provide a sample of implementing a CopyTo(..) and Clone() method.
I do not understand with the CopyTo(...) method, why the object passed to it isnt passed as a ref or out parameter. Changes will not hold outside of call otherwise.
Any help would be appreciated.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks maliger for your great help.
Few points:
* Cannot use "this" as a reference parameter.
* Business Objects - iff the key is provided in the constructor, I want to load all values from the database into the current business object - this is quite common. Again, ONLY if the key is provided in the constructor. As such, I need to get the values and map all properties.
* MemberwiseClone - I have seen this before in some samples and your comments have helped me GREATLY as I was unaware as to what this method did. THANKS!!
Have you any samples of implementing the Clone() and CopyTo(BusinessObject obj)??
Notice that the business object is not passed by ref or out to the CopyTo method - this is how I have seen it implemented - how then does the object passed to the method, get updated and retained.
Cheers.
Sorry about the ref parameter. "ref object" of yours Get method is what confused me. I don't know what this method exaclty do and its hard to provide some hints without this knowledge.
Here I suppose some solution. object are automatically passed by "reference" - meaning that the callee can change the inner fields/properties of the passed object (if they are visible). But passing as object do not solve much - you still need to cast it to proper type to be able fill it. What about to change it to typesafe Get and provide overload for every type it can load?
Notice: The database stuff is just a mock. Do not use that in real life! :-)
As another note, I'd suggest quite diifferent route (which I believe is followed more common in .net world). The database loader class returns just DataSet class (prefferably typed dataset) and your BL class (Itinerary) simply fills its private members with data it obtains in that dataset.
So - consider you got ItenerariesDataset class
and
ItenerariesDataset.Itenera
You can then have something like this:
Business Accounts
Answer for Membership
by: maligerPosted on 2008-07-11 at 00:45:12ID: 21980600
If you have all members just basic types (int, string, etc.) and no references to other objects, you can use
wlink/?Lin kId=64165)
MemberwiseClone method.
Every object got that.
Regarding the this setting:
I never saw that and guess its not possible.
Here is exerpt from c# specification (http://go.microsoft.com/f
· When this is used in a primary-expression within an instance constructor of a class, it is classified as a value. The type of the value is the instance type (§10.3.1) of the class within which the usage occurs, and the value is a reference to the object being constructed.
· When this is used in a primary-expression within an instance constructor of a struct, it is classified as a variable. The type of the variable is the instance type (§10.3.1) of the struct within which the usage occurs, and the variable represents the struct being constructed. The this variable of an instance constructor of a struct behaves exactly the same as an out parameter of the struct typein particular, this means that the variable must be definitely assigned in every execution path of the instance constructor.
If I read it well, it means that in struct it should be possible?!
I'd prefer the cloning way for sanity and managebility.