Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2004

Tip: Looking for answers? Try searching our database.

Strongly-typed CopyTo() implementation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
- 29 Jun 2004 14:05 GMT
I have a collectiond derived from NameObjectCollectionBase. FxCop is
complaining that I need to implement a strongly typed CopyTo(MyObjectType,
int)

How do I do this? I can't seem to find any implementations of CopyTo() that
don't rely on Array.Copy or some other internal .NET method. Unfortunately,
there's nothing I can find that will work for a NameObjectCollectionBase
derived class. I can't find any implementations of CopyTo() for
NameObjectCollectionBase derived classes out on google either.

The documentations doesn't really explain how CopyTo() is supposed to
function. The one thing that appears missing is an explanation of what
you're supposed to do if the index passed in is mid-way into the array. Do
you shift all the items out to make room or do you overwrite existing items?
I presume you shift them out.

I tried seeing how .NET implements it in similar collections using
Reflector. Unfortunately, all the implementations appear to be written in
unmanaged code, so I can't see how they're implemented.

Can anyone help me on this? I need to do it, I presume, for both the Keys
and the Values members. Also, there's no Insert() functionality in
NameObjectCollectionBase, so I'm not sure how to shift the elements out.

Thanks.

Pete Davis
Richard A. Lowe - 29 Jun 2004 14:47 GMT
Hi Pete, Why would you believe that you couldn't/shouldn't use an internal,
Object-typed method in the implementation of your strong-typed CopyTo
method?  I believe all FXCop wants is for you to add an overload of CopyTo
that specifies the array type to copy to.  In your case this would be
DictionaryEntry, or some custom class that specified a string key and object
value.

BTW, the only CopyTo behaviour I've ever seen overwrites any items in the
target array, and makes no attempt to re-create a new array if the target
array isn't big enough, however that may not always be the case.

Richard
Signature

C#, .NET and Complex Adaptive Systems:

> I have a collectiond derived from NameObjectCollectionBase. FxCop is
> complaining that I need to implement a strongly typed CopyTo(MyObjectType,
[quoted text clipped - 23 lines]
>
> Pete Davis
- 29 Jun 2004 16:03 GMT
The underlying implementation of CopyTo() in NameObjectCollectionBase is
private, so I can't call down to it. There's no implementation in the
underlying base class to handle this. The keys and values are collections
(not arrays) that also don't support CopyTo(). So how do I write a
CopyTo()????

That's my question. The destination array can't be pre-allocated to be large
enough since ever value must have a unique key (I believe. Maybe I'm wrong
on this. I'll check). So I'd have to use Add() to add new values (since
there's no Insert). But if I'm doing a CopyTo() mid-array, I guess I'd use
the indexer to overwrite, according to what you're saying. Is that correct?

Pete

> Hi Pete, Why would you believe that you couldn't/shouldn't use an internal,
> Object-typed method in the implementation of your strong-typed CopyTo
[quoted text clipped - 38 lines]
> >
> > Pete Davis
Richard A. Lowe - 30 Jun 2004 14:49 GMT
Signature

C#, .NET and Complex Adaptive Systems:

> The underlying implementation of CopyTo() in NameObjectCollectionBase is
> private, so I can't call down to it. There's no implementation in the
> underlying base class to handle this.

The implementation is not visible if you have a variable of type
NameObjectCollectionBase, but NameObjectCollectionBase  implements
ICollection.  Simply cast your reference of NameObjectCollectionBase to
ICollection and you'll have a base CopyTo implementation that you can use to
implement your strongly-typed CopyTo.

I.e. (warning: air code, extra work may be required)

classs MyNameCollection : NameObjectCollectionBase
{
   // this should shuttup FXCop!:
   public void CopyTo(MyObjectType[] array, int index)
   {
       ((ICollection)this).CopyTo(array, index);
   }

Now you just let CopyTo deal with the specifics of the copying.  Since the
array reference is passed in by value, there's no way to "swap out" the
original array that's being passed in, so you shouldn't worry about whether
the target array is large enough.  If it isn't, CopyTo should throw an
ArgumentException.  It's the responsibility of the caller to make sure the
array is large enough.

If you want a CopyTo-style method that does 'grow' the array as needed, you
should create a different method with this signature (or have the user pass
in an ArrayList):

public void InflatableCopyTo(ref MyObjectType[] array, int index)
{
// code to grow array here.
}

Since you can't 'grow' an array, you have to re-allocate and swap out the
old one for the new one, you'll need a ref or out on the parameter to tell
the caller you will (potentially) be changing the array.

HTH,
Richard

> The keys and values are collections
> (not arrays) that also don't support CopyTo(). So how do I write a
[quoted text clipped - 56 lines]
> > >
> > > Pete Davis

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.