Clear Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Schubert

    Clear Method

    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.
  • William Ryan

    #2
    Re: Clear Method

    Clear is a method native to the collectionbase class...everyth ing inherited
    from it can implement it. It's actually an implementation of the
    IList.Clear interface.


    Here's a link about CollecitonBase


    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..althou gh 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 Schubert" <bschubert@msn. com> wrote in message
    news:0b3f01c37d 47$2e91c140$a00 1280a@phx.gbl.. .[color=blue]
    > 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.[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Clear Method

      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.



      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

      "William Schubert" <bschubert@msn. com> wrote in message
      news:0b3f01c37d 47$2e91c140$a00 1280a@phx.gbl.. .[color=blue]
      > 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.[/color]


      Comment

      • William Ryan

        #4
        Re: Clear Method

        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
        FileSystemWatch er, 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
        "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
        news:uHxgGmUfDH A.560@tk2msftng p13.phx.gbl...[color=blue]
        > William,
        > To the best of my knowledge Clear is only used in Collections to remove[/color]
        all[color=blue]
        > 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[/color]
        other[color=blue]
        > 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[/color]
        &[color=blue]
        > the constructor (yes I would hope both call the same routine). However[/color]
        there[color=blue]
        > 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[/color]
        see[color=blue]
        > if there is another definition for Clear.
        >
        >[/color]
        http://msdn.microsoft.com/library/de...Guidelines.asp[color=blue]
        >
        > 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
        >
        > "William Schubert" <bschubert@msn. com> wrote in message
        > news:0b3f01c37d 47$2e91c140$a00 1280a@phx.gbl.. .[color=green]
        > > 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.[/color]
        >
        >[/color]


        Comment

        • Parker Zhang [MSFT]

          #5
          RE: Clear Method

          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.

          --
          Parker Zhang
          Microsoft Developer Support

          This posting is provided "AS IS" with no warranties, and confers no rights.

          Comment

          • William Schubert

            #6
            RE: Clear Method

            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.Cl ear().

            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.


            [color=blue]
            >Hi William,
            >
            >Do you think the posts from William Ryan and Jay B.[/color]
            Harlow answer your[color=blue]
            >questions?
            >
            >If you have any Qs, please reply to this post.[/color]


            Comment

            Working...