- Home /
MemberWiseClone Protected?!?
Hello guys, Ive been trying to get this code to work for a few hours and I am really stumped. I get the error:
Assets/Scripts/ItemHolder.cs(122,36): error CS1540: Cannot access protected member object.MemberwiseClone()' via a qualifier of type
ClassHolder.Item'. The qualifier must be of type `ItemHolder' or derived from it
ItemHolder is just a big database of items set up like :
item[0].name = "Coin"; item[0].maxStack = 10000; item[0].type = "Currency"; item[0].ID = 0; item[0].stats.baseValue = 1;
The error lines are:
void ReturnItem(int id)
{
ClassHolder.Item passon = item[id].MemberwiseClone(); ///Error Line
return passon;
}
Im assuming the issue is that i dont have access to MemberWiseClone; Which I am guessing is a system class. What could be a solution is this issue.
Answer by Julien-Lynge · Mar 09, 2013 at 01:33 AM
So as I understand it from the documentation (http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx) and your description, MemberwiseClone is a protected class, which means that it's private to a class and all its children. That means that it is not a public class and cannot be called from outside the class.
That's precisely what you're trying to do here - call it on a ClassHolder.Item from inside another class (ItemHolder presumably). If you want to be able to do a shallow clone using this method, you have to add a public method or property to ClassHolder.Item and then have that method or property do the clone. Example:
public WhateverClass Clone()
{
return (WhateverClass)this.MemberwiseClone();
}
I moved the code to the ClassHolder, but i still get the same error. Am i missing something?
I'm not sure what your code structure is, but if Item is a class you would have to move it there - basically, you have to move it to the class that defines whatever item[] is an array of.
Looking at your error message it appears you have a type mismatch going on. Am I right in assu$$anonymous$$g that the item[] array is a ItemHolder, and you're trying to assign it to a ClassHolder.Item? Have you defined an explicit or implicit conversion between ItemHolder and ClassHolder.Item?
Perhaps try:
ItemHolder temp = item[id].$$anonymous$$emberwiseClone();
ClassHolder.Item passon = (ClassHolder.Item)temp;
if you've defined an explicit conversion.
At this point you may need to include some more code. At the very least I need to know what type that array is, and it might be helful just to give me the definitions for those two classes.
Thanks, that's helpful.
Next question: Does ClassHolder.Item derive from ItemHolder, or ItemHolder derive from ClassHolder.Item? Why don't you just upload the entire script as an attachment so I can look throught it? Otherwise we might go around in circles for a while.
Sounds good. In the meantime I suggest you read up on the error you're seeing (the CSXXXX is an error code, and you can always google them):
http://msdn.microsoft.com/en-us/library/s9zta243.aspx
All I'm going to be doing is trying to see if your code does what's in the example there.
Your answer
Follow this Question
Related Questions
Deleting an instance 2 Answers
How do I properly duplicate an object in a editor script? 3 Answers
I dont want "(clone)" but how to change? 2 Answers
Unity object cloning and public/private variables 0 Answers
save/load cloned game object 1 Answer