473,326 Members | 2,196 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,326 software developers and data experts.

Can i retrieve all properties in a class to copy one object to another?

Basically I want to do something like this (psuedo-code):
you have:
MyClass MyObject;
which is instantiated in the form load.
You have a copy function:
{
MyClass MyObjectCopy = new MyClass(id);
foreach (property p in MyClass.properties)
{
MyObject.p = MyObjectCopy.p;
}

}
This copy function obviously won't work as is. What would be the
correct way to simulate what I am trying to do here, or at least some
hints to get me started?

Thanks,
Mike

Jan 13 '06 #1
5 1571
"Mi************@gmail.com" <Mi************@gmail.com> wrote in
news:11**********************@g44g2000cwa.googlegr oups.com:
This copy function obviously won't work as is. What would be the
correct way to simulate what I am trying to do here, or at least some
hints to get me started?


If you just want to do this for MyClas (or other specific classes that you
define) then a better way would be to implement IClonable on the classes.

-mdb
Jan 13 '06 #2
Thanks for the response!

Thing is, I don't want an exact copy... just to copy the values of the
public properties. I want the values of the private variables to stay
the same. I probably should have mentioned that in the OP.

That's why I thought the foreach way would be a good way to implement
this... the question is foreach ? in ?

Jan 13 '06 #3
On Fri, 13 Jan 2006 12:58:13 -0600, Mi************@gmail.com
<Mi************@gmail.com> wrote:
Thanks for the response!

Thing is, I don't want an exact copy... just to copy the values of the
public properties. I want the values of the private variables to stay
the same. I probably should have mentioned that in the OP.

That's why I thought the foreach way would be a good way to implement
this... the question is foreach ? in ?


look at System.Reflection classes....the thing to iterate over is gotten
by calling GetProperties():

http://msdn.microsoft.com/library/de...tiesTopic1.asp

--
Craig
Microsoft MVP - ASP/ASP.NET
Jan 13 '06 #4
"Mi************@gmail.com" <Mi************@gmail.com> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
Thing is, I don't want an exact copy... just to copy the values of the
public properties. I want the values of the private variables to stay
the same. I probably should have mentioned that in the OP.


I can't for the life of me figure out why you would want to do this. First
of all, the state of the resulting object will likely be inconsistent.
Second, how do you know that some of the properties aren't read only?
Third, some might question the wisdom of "public properties" in the first
place.

If you are trying to do this just "to see if you can" then fine... its an
academic experiment. You can probably do something like this (untested):

MyClass m = new MyClass();
MyClass m2 = new MyClass();
foreach(PropertyInfo pi in m.GetType().GetProperties())
{
if (!pi.CanWrite || !pi.CanRead) continue;

PropertyInfo pi2 = m2.GetType().GetProperty(pi.Name);
pi2.SetValue(pi.GetValue());
}

-mdb

Jan 13 '06 #5
Thank you very much!!

My new copy function:
{
MyClass MyObjectCopy = new MyClass(id);

foreach(PropertyInfo pi in MyObject.GetType().GetProperties())
{
if (pi.CanWrite)
{
pi.SetValue(MyObject, pi.GetValue(MyObjectCopy, null), null);
}
}
}
I can't for the life of me figure out why you would want to do this.


Basically, I have my own custom class. There are private variables with
identifiers. There are public read-only properties that are binded to
labels. There are other public properties binded to editable controls.
The user can open up a dialog box, select the id number of the thing
they want to copy from. The new object gets created. And now my
origional object's writeable properties are overwritten with the values
from the thing the user wanted to copy from.

Jan 13 '06 #6

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

Similar topics

2
by: James Thurley | last post by:
I'm having a problem figuring out the best way to use Properties in my code. Here are the 'good practices' I have in my head: 1) Variables should always be accessed through Properties where...
22
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
9
by: Don | last post by:
Say I have a class like so: Public Class MyClass Public Prop1 as Integer Public Prop2 As Integer Public Prop3 As Integer End Class Is it possible to retrieve a list of the variables or...
8
by: active | last post by:
Guess I'm looking for someone who likes to work difficult puzzles. I can't seem to ever retrieve a palette handle from the clipboard. Below is a simple test program that demonstrates the...
10
by: Timothy | last post by:
Hi, I was looking over some differences between C# and VB code today and noticed that the set method in properties act completely different. C# values are passed by reference, and VB values are...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
2
by: Cramer | last post by:
Using ASP.NET 3.5... As far as I know, any time we store a value in application or session state, it is stored as a humble 'object' type rather than it's "real" type. For example, if we want...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.