Create shallow copy of CollectionBase inherited class

S

Stefan Hoffmann

hi,

as i'm farely new to C#, i have some problem of creating a shallow copy
of a class inherited of CollectionBase.

As far as i see, i just need to copy the CollectionBase.List using the
CopyTo method.

Here i'm stuck:

public class myCollection : System.Collections.CollectionBase
{

protected myCollection()
{
}

protected int Add(object value)
{
if (value != null)
return List.Add(value);
else
return -1;
}

protected void Insert(int index, object value)
{
if (value != null)
List.Insert(index, value);
}

protected object this[int index]
{
get
{
return List[index];
}
set
{
Insert(index,value);
}
}

public void CopyTo(Array array, int start)
{
List.CopyTo(array, start);
}

}


Using

myCollection originalCollection = new myCollection;
myCollection shallowCopy = new myCollection;

originalCollection.CopyTo(shallowCopy, 0);

won't work.

I need some clues...


mfG
--> stefan <--
 
S

Stefan Hoffmann

hi,

Stefan said:
public class myCollection : System.Collections.CollectionBase
{
protected myCollection()
{
}
Using

myCollection originalCollection = new myCollection;
myCollection shallowCopy = new myCollection;

originalCollection.CopyTo(shallowCopy, 0);

won't work.
This is the real inheritence chain:

class NullSafeCollection: System.Collections.CollectionBase {}
class StraightSegmentList: NullSafeCollection {}

Using

StraightSegmentList originalSegments;

StraightSegmentList copy = new StraightSegmentList();
originalSegments.CopyTo(copy, 0);

doesn't compile.


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top