473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Copy Values From One Object to Another

GM
Hi,

I have 2 objects A and B. I want to copy only the values of B across to A
without changing the reference of A. Is there a way to achieve this without
the use of reflection?

Cheers,

Graham
Nov 15 '05 #1
2 6907
GM,

You don't have to use reflection. You should just have a method that
takes an instance of your type, and sets the values internally.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"GM" <ma*****@gaelquality.co.uk> wrote in message
news:Ok****************@tk2msftngp13.phx.gbl...
Hi,

I have 2 objects A and B. I want to copy only the values of B across to A
without changing the reference of A. Is there a way to achieve this without the use of reflection?

Cheers,

Graham

Nov 15 '05 #2
GM
Thanks for the reply. I should have explained it a bit better though.

I am trying to generalise how I do this so that the method for doing this
takes in 2 objects eg -
ApplyValues(object A, object B);
but the class containing this method does not care what the underlying type
of the objects are. So what I would really like to be able to do would be
to write something like:
Object.ApplyValues(object A, object B);

but I don't believe such a method exists.

If I wrote a method on each of my types that took another instance of that
type and set the values internally I'm not sure how I could cast the objects
to that type in order to call that method. Perhaps I need some kind of
interface which just contains an ApplyValues(method) implemented in my
custom types?

Thanks again,

Graham

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2***************@TK2MSFTNGP12.phx.gbl...
GM,

You don't have to use reflection. You should just have a method that
takes an instance of your type, and sets the values internally.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"GM" <ma*****@gaelquality.co.uk> wrote in message
news:Ok****************@tk2msftngp13.phx.gbl...
Hi,

I have 2 objects A and B. I want to copy only the values of B across to A without changing the reference of A. Is there a way to achieve this

without
the use of reflection?

Cheers,

Graham


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
10
by: ambar.shome | last post by:
Dear All, Whats the difference between a copy constructor and assignment operator. We can assign the values of member variables of one object to another object of same type using both of them....
7
by: Harag | last post by:
Hi all If I create an object with the following: var ob1 = new objMyDefinedObject(); then I assign it to a new variable. var ob2 = ob1
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
6
by: pnp | last post by:
I have two objects AA and AB that derive from object A. These two objects have common atts that they inherit from object A but also some of their own. I have a pointer to an AA object and i...
2
by: Joao Dias | last post by:
Hi, I am almost new here and a newbie in ASP.NET and .NET in general. How can I make just that ? I have tried at least 5 different ways of doing that and I just can't: I have a data-bound...
7
by: Jeffrey Barish | last post by:
(Pdb) myclass MyClass( 0, 0, 'A string', 123.45) (Pdb) copy.copy(myclass) *** TypeError: TypeError('__new__() takes at least 4 arguments (2 given)',) I see 4 arguments (actually, 5 because...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.