Clear Method

  • Thread starter Thread starter William Schubert
  • Start date Start date
W

William Schubert

Is there a common defintion of Clear that applies to both
objects and collections? If so, where is it documented.

My preconceived notion is that Clear means "reset the
object to its initial state" and this applies to both
collections and objects. The problem is that I cannot
find anything in the literature supporting this. That
would be ok, but I cannot find anything at all.
 
Clear is a method native to the collectionbase class...everything inherited
from it can implement it. It's actually an implementation of the
IList.Clear interface.


Here's a link about CollecitonBase
http://msdn.microsoft.com/library/d...mcollectionscollectionbaseclasscleartopic.asp

Not all objects have a Clear method, and in many instances it wouldn't make
sense to have one. What's the difference? Well, I could have an object
like a DataTable comprised of Rows and Columns which are both collections.
in this instance, dt.Clear is a default for dt.Rows.Clear....it doesn't
clear out the columns..although one might argue it should . In this sense,
I have a datatable, add rows and columns, populate it with data, then call
clear, I still have rows. IF I was setting it back to its original state,
I'd have neither.

HTH
 
William,
To the best of my knowledge Clear is only used in Collections to remove all
the contained items. Not necessarily to an initial state. In that the
collection may have an initial capacity, but Clear does not return the
collection (internally) back to this initial capacity.

My problem with Clear applying to objects, is that on a lot of objects other
than creating a new instance of said object there is not clear 'initial
state' (no pun intended). As you are duplicating logic in the Clear method &
the constructor (yes I would hope both call the same routine). However there
are object that do have a concept of Clear, see MSDN index note below.

You could check the "Design Guidelines for Class Library developers" to see
if there is another definition for Clear.

http://msdn.microsoft.com/library/d...ef/html/cpconNETFrameworkDesignGuidelines.asp

Or you could use the MSDN Index to look up the Clear method and see where
all its applied. Of course you need to hit the one dealing with the .NET
Framework. ;-)

Hope this helps
Jay
 
Jay:

Agreed, I probably muddled my point. He had asked about the initial state
thing and I guess what I was trying to say is that everything has an
initials state when you just declare and instantiate it. It makes a lot
of sense to have a Clear with collections, but like a Winform or something,
Clear woudln't make much sense. Tons of other objects, like
FileSystemWatcher, CrystalReport etc wouldn't be very good candidates for
clear. However, if they have properties that are collections, then those
would always be good candidates. Ultimately, I was trying to make the point
that Clearing to the initial state would be a big problem in many cases.

As an aside, muchos gracias for the Design Guidelines link....I defnintely
need to take a gander it.

Thanks again,

Bill
 
Hi William,

Do you think the posts from William Ryan and Jay B. Harlow answer your
questions?

If you have any Qs, please reply to this post.
 
Actually, not entirely. Upon reading the definintions of
a large number of the clear methods in the framework, I
see threee basic categories:
1. Clear releases the resources for the object
2. Clear resets the object to an initial state
3. Clear removes all elements of the collection, but
does not appear to reset the state. (Clear is a big part
of CollectionBase derived classes, as one would suspect.)

My coworker's concern was that he believed that a call
to "Clear()" on a collection associated with a database,
followed by a save, should essentially remove all the
associated records from the database. Essentially,
Clear() = RemoveAll(). Thus he believes the meaning
of "Clear()" is not consistent among the objects in
the .Net framework.

My impression was that "Clear()" was used to reset the
object to an initial state, so I use "Clear()" in my
configuration application when changing tasks, or prior
to exit, much as the DOS command CLS was used when I was
much younger. If Clear means "reset the object to an
initial state", then IList.Clear() is essentially the
same as Graphics.Clear() or CryptoStream.Clear().

The result of the confusion, as you might expect, is an
application that raises the "I am dirty, do you want to
save?" message, and if the user selects "Yes", all the
records in the database are deleted! Thus we require
some clarification (believe me, I am trying to be careful
not to use the word "Clear" here) as to whether or not
there is a generalized definition and usage for this
method call.

Thank you to all for the clarifications to date.
 
Back
Top