473,382 Members | 1,665 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.

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 1575
"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
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.