• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C# memberwise clone in constructor

Bohica69

Gawd
Joined
Jul 12, 2005
Messages
676
I have a need to do a memberwise clone in a constructor - for some reason it's not working. Here's the code:

Code:
public Fee(Fee BaseFee)
        {
            // Make a shallow copy of the BaseFee
            this = (Fee)BaseFee.MemberwiseClone();
            
            // Set the things we know aren't going to be added to the fees.
            _admin = 0;
}

I'm getting the message that Cannot assign to '<this>' because it is read-only ? Any ideas?

Thanks.
 
I dont know a lot about C# but why do it this way, are you not able to access the member variables of BaseFee? would it not be easier to just copy the objects directly since you are in a copy constructor?

Code:
   var = BaseFee.var;
 
Yes, I could do that, but doing it this way is one line of code vs about 75 (to cover each member variable)

Just trying to save on the drudge work.
 
C# will not allow you to reassign the value of 'this.'
Typically you will use something like Memberwise Clone in place of regular constructor, not inside of it.

Code:
Fee newfee = null
newfee = (Fee)oldFee.MemberwiseClone()

should work for what you want, performing a shallow copy of oldFee and assigning the new clone to newFee.

Alternatively, if you need a copy constructor:
Code:
using System.Reflection
        public Fee(Fee oldFee)
        {
            FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            foreach (FieldInfo fi in fields)
            {
                fi.SetValue(this, fi.GetValue(oldFee));
            }
        }

performs the same shallow copy that MemberwiseClone() does, using reflection allows you to peform the copy from inside a Fee constructor becuase you don't need to assign 'this' to a cloned object, you're just setting the member variables.

I don't know how MemberwiseClone is implemented, but I do that Reflection is quite slow, so if any of this takes place in time critical sections enumerating the fields to copy would be faster. Even though doing so takes a while to type out and requires updating if you alter the fields of the object later.
 
FreiDOg,

Thanks for the reflection code - I REALLY need to spend some time learning more of the intricacies of reflection....

Anyway what I ended up doing was implimenting the ICloneable interface in Fee
public object Clone()
{
return this.MemberwiseClone();
}

Then I wrote a method ChangeToOtherVersion() that does the changes I need.
Thanks for the help.
 
Back
Top