Re: MemberwiseClone() doesn't like arrays?



On 10 Mar, 10:24, Marc Gravell <marc.grav...@xxxxxxxxx> wrote:
Had to struggle to remember...

First - we were talking about arrays, not ArrayList - there is a
*huge* difference.

Oops, my bad for not reading the first post properly :x

To clone an ArrayList you would call Clone() - this
will internally worry about "doing the right thing" i.e. creating a
separate array. However, this can't know about your new type, so you
will need to write Clone yourself, as below.

OK, I figured I could go that route, but I *Really* want to avoid that
as I have lots of classes inheriting from ArrayList, each with lots of
members. Further, I have lots of classes inheriting from those
classes, adding more members still. I was hoping for a more-readily
maintained solution, more along the lines of reflection, which could
say something like

<pseudocode>
public override object Clone()
{
ThingList clonedThingList = new ThingList();
foreach(Thing th in this)
{
clonedThingList.Add(th);
}
foreach(member_of_ThingList somemember)
{
clonedThingList.somemember = this.somemember;
}
return clonedThingList;
}

</pseudocode>
where the second foreach is, say, using reflection to find each member
aggregated in, such as ThingList.Caption, ThingList.Text, ThingList.X,
ThingList.Y, etc or whatever.

Am I talking utter nonsense?

Second; yes - the question of deep vs shallow copy is one that I
myself commented on:

Yep, followed all that and I *want* the items in my cloned arraylist
to be shallow copies; I'm more than happy with that bit.



Me: <quote>
The point I was trying to
make, however, is that if you have a class inside the array, you
might
actually want to clone that too! Or you might not, it depends on the
scenario.</quote>

By default you have a shallow copy; this is expected. If you want a
deep clone you'll have to do it yourself. In 2.0 you could perhaps
insist that the items are cloneable.

Yep, fine with all that.


By the way, I swapped the public field for a property; you might also
want to avoid using ArrayList if you are in 2.0; generics and things
like List<T>/Collection<T> largely deprecate ArrayList.

I intend to keep ArrayList for a variety of reasons, not least because
there's a stack of code already in place. However, I also need
something to build down to .net CF 1.1 (although the clone
functionality need not).


<snip>

Thanks
.



Relevant Pages

  • Re: visibility matter on converting object to array
    ... i'll get an array having keys that from member variable's name ... i guessed "i can get public members but not protected, private, static ... Is it right way to access object members?(by converting to ...
    (comp.lang.php)
  • Re: Array Design
    ... ITOH it's pretty sure you are referencing an element in the array that you ... > When the application starts I initialise a state class, ... > I ask the user how many members to create. ... >>> have exceptions cropping up. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: extract items from list in single field
    ... Try using the Splitfunction to change the delimited string into an array. ... You would then loop through the members of the array and write one record to ...
    (microsoft.public.access.modulesdaovba)
  • Re: how to make a variable based on array length and member value
    ... I have an array full_arrayof an arbitrary number of transaction ... From the array I have created a new array years.uniq! ... what id like to do is take the new arrays members and use them as ... will fail, because you HAVE NOT YET WRITTEN THE CODE. ...
    (comp.lang.ruby)
  • RE: Xml serialization, arrays, XmlAnyAttribute
    ... an array type property/member. ... The use case I was trying to implement involved adding an xml attribute to ... .NET class's static members, correct? ... the .NET Xmlserializer based xml serialization ...
    (microsoft.public.dotnet.framework)