[RESOLVED] Simple question about objects...
I have a class:
VB Code:
Public Class MyClass
Public myString as String
Public myInt as Integer
End Class
and 2 instances of this class:
VB Code:
Dim o1, o2 As New myClass
If i write "o1 = o2" this will set o1 as a reference to o2 object, even if I allocated different memory segments for each one of them, right?
So, the question is how do I copy the information from o2 into o1. Something like:
VB Code:
o1.myString = o2.myString
o1.myInt = o2.myInt
This code works, but if I would have had 100 fields in that class... how would I have done this?
Thank you.
Re: Simple question about objects...
You can call the MemberwiseClone method that all objects have or you can implement the ICloneable interface and provide your own implementation of the Clone method. Other than that, the only way to copy all properties of one object to the other without coding each individual property would be using reflection.
VB Code:
For Each [property] As PropertyInfo In o2.GetType().GetProperties()
[property].SetValue(o1, [property].GetValue(o2, Nothing), Nothing)
Next [property]
Re: Simple question about objects...
Didn't the code I gave you last night work?
Re: Simple question about objects...
Quote:
Originally Posted by jmcilhinney
You can call the MemberwiseClone method that all objects have or you can implement the ICloneable interface and provide your own implementation of the Clone method. Other than that, the only way to copy all properties of one object to the other without coding each individual property would be using reflection.
VB Code:
For Each [property] As PropertyInfo In o2.GetType().GetProperties()
[property].SetValue(o1, [property].GetValue(o2, Nothing), Nothing)
Next [property]
This works only for properties, as you know... I need to change the members, too. I tried to change the code, but for members I don't have any method like .SetValue. Can you do this with properties, too?
Quote:
Originally Posted by sevenhalo
Didn't the code I gave you last night work?
Yes, it did... But I didn't really understand how it works, and here I am asking a similar question and getting a similar answer... Sorry for that.
Re: Simple question about objects...
What do you mean by "members"? A property is a member, as is a method, a field, etc. If you mean fields, i.e. class-level variables, then you can use the FieldInfo class in place of the PropertyInfo class. There is also the more general MemberInfo class, although I've never used it so I don't know if it can be used to get and set property and field values.
Also, keep in mind that it is the .NET convention to use a private variables and expose them through public properties, rather than just exposing a public field.
Re: Simple question about objects...
Reflection is not the ONLY way ;)
You can simply Binary serialize the object to a memory stream and then deserialize it back out into an other variable and you'll end up with a perfect (deep) clone.
Its much easier than messing around with reflection.
Re: Simple question about objects...
Quote:
Originally Posted by jmcilhinney
What do you mean by "members"? A property is a member, as is a method, a field, etc. If you mean fields, i.e. class-level variables, then you can use the FieldInfo class in place of the PropertyInfo class. There is also the more general MemberInfo class, although I've never used it so I don't know if it can be used to get and set property and field values.
Also, keep in mind that it is the .NET convention to use a private variables and expose them through public properties, rather than just exposing a public field.
Yes, I meant fields, sorry about my english... :ehh: Just one more question about your last point: I understand the theoretical difference between declaring a variable private and accesing it through a property, on one hand, and declaring it public on the other hand, but why does it matter so much? I'm a student, not a programmer like you (I suppose) and I'd like to know if it really matters how do you handle this practical. Thank you.
Quote:
Originally Posted by wossname
Reflection is not the ONLY way ;)
You can simply Binary serialize the object to a memory stream and then deserialize it back out into an other variable and you'll end up with a perfect (deep) clone.
Its much easier than messing around with reflection.
I have never serialized an object using .NET, but I remember I've did this once in Java and it was a little bit more complicated than a few lines of code, like jmcilhinney said... If I remember well, we use serialization of objects when we want to store an object and use it to pass a parameter to a remote application or to implement object persistence (what I want now). This is useful only if you want to store an object and use it after you close and restart your application, right? Correct me if I'm wrong...
PS: Sorry about answering so late, I was in the Easter holiday for a few days...
Re: Simple question about objects...
Here's a quote from the MSDN library:
Quote:
Properties and fields both store and retrieve information in an object. Their similarity can make it difficult to determine which is a better programming choice in a given case.
Use property procedures when:
You need to control when and how a value is set or retrieved.
The property has a well-defined set of values that need to be validated.
Setting the value causes some perceptible change in the object's state, such as a visible property.
Setting the property causes changes to other internal variables or to the values of other properties.
A set of steps must be performed before the property can be set or retrieved.
Use fields when:
The value is of a self-validating type. For example, an error or automatic data conversion occurs if a value other than True or False is assigned to a Boolean variable.
Any value in the range supported by the data type is valid. This is true of many properties of type Single or Double.
The property is a String data type, and there is no constraint on the size or value of the string.
One of the advantages of properties that is not mentioned there is that they allow you to change your implementation at a future time without changing the interface exposed by your type, thus not breaking any existing code that uses it. I use public properties almost exclusively for this reason.
Re: Simple question about objects...
I would rate you at least once a day if I could... Thank you very much.