PC Review


Reply
Thread Tools Rate Thread

Create shallow copy of CollectionBase inherited class

 
 
Stefan Hoffmann
Guest
Posts: n/a
 
      15th Jan 2007
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 <--

 
Reply With Quote
 
 
 
 
Stefan Hoffmann
Guest
Posts: n/a
 
      15th Jan 2007
hi,

Stefan Hoffmann schrieb:
> 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 <--

 
Reply With Quote
 
Mike Powell
Guest
Posts: n/a
 
      15th Jan 2007
Hi Stefan,
try using the Clone() method, this creates a shallow copy of the collection
Mike Powell
www.ramuseco.com

"Stefan Hoffmann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> hi,
>
> Stefan Hoffmann schrieb:
>> 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 <--
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Expose Count from System.Collections.CollectionBase in an inherited class GoCoogs Microsoft VB .NET 2 20th Jul 2006 07:50 PM
Is it possible to create virtual class to force the class must inherited? ABC Microsoft C# .NET 1 11th Jan 2006 11:35 AM
Why I can't create a contructor in a class inherited from Component Model class C.G. Oh Microsoft Dot NET Framework 0 19th Nov 2004 02:09 PM
implementing inherited control re:Ken Tucker - Inherited DataGrid Class Doug Bell Microsoft VB .NET 2 6th Oct 2004 10:07 PM
custumize serialization & CollectionBase inherited class wael radwan Microsoft C# .NET 2 24th Nov 2003 08:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:42 AM.