Passing a Derived Object as a Reference Parameter

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

    Passing a Derived Object as a Reference Parameter


    How can I create a method that will take any object derived from a
    base class as a reference parameter?

    Something like this:

    void DestroyObject(r ef BaseObject obj)
    {
    obj.Dispose();
    obj = null;
    }

    DestroyObject(r ef derivedObject1) ;
    DestroyObject(r ef derivedObject2) ;
    DestroyObject(r ef derivedObject3) ;

    Where derivedObject1, 2 and 3 are of a class type derived from
    BaseObject?

    Thanks....

    --Bruce

  • Joanna Carter \(TeamB\)

    #2
    Re: Passing a Derived Object as a Reference Parameter

    "Bruce" <bvanderw-news5021@mailbl ocks.com > a écrit dans le message de news:
    lr4b419mcdou87v 1hq9a495isurs92 brf7@4ax.com...
    [color=blue]
    > Something like this:
    >
    > void DestroyObject(r ef BaseObject obj)
    > {
    > obj.Dispose();
    > obj = null;
    > }
    >
    > DestroyObject(r ef derivedObject1) ;
    > DestroyObject(r ef derivedObject2) ;
    > DestroyObject(r ef derivedObject3) ;[/color]

    Yes, but I have to ask why you are explicitly disposing of objects instead
    of letting the garbage collector do its work ? Or do your classes hold
    references to Windows resources or file handles?

    Joanna

    --
    Joanna Carter
    Consultant Software Engineer


    Comment

    • Joanna Carter \(TeamB\)

      #3
      Re: Passing a Derived Object as a Reference Parameter

      "Bruce" <bvanderw-news5021@mailbl ocks.com > a écrit dans le message de news:
      lr4b419mcdou87v 1hq9a495isurs92 brf7@4ax.com...
      [color=blue]
      > Something like this:
      >
      > void DestroyObject(r ef BaseObject obj)
      > {
      > obj.Dispose();
      > obj = null;
      > }
      >
      > DestroyObject(r ef derivedObject1) ;
      > DestroyObject(r ef derivedObject2) ;
      > DestroyObject(r ef derivedObject3) ;[/color]

      Yes, but I have to ask why you are explicitly disposing of objects instead
      of letting the garbage collector do its work ? Or do your classes hold
      references to Windows resources or file handles?

      Joanna

      --
      Joanna Carter
      Consultant Software Engineer


      Comment

      • Mattias Sjögren

        #4
        Re: Passing a Derived Object as a Reference Parameter

        >Where derivedObject1, 2 and 3 are of a class type derived from[color=blue]
        >BaseObject?[/color]

        BaseObject bo = derivedObject1;
        DestroyObject(r ef bo);

        But I don't understand why you use a ref parameter. If you used a by
        value parameter, you could pass in derivedObject1 directly.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • Mattias Sjögren

          #5
          Re: Passing a Derived Object as a Reference Parameter

          >Where derivedObject1, 2 and 3 are of a class type derived from[color=blue]
          >BaseObject?[/color]

          BaseObject bo = derivedObject1;
          DestroyObject(r ef bo);

          But I don't understand why you use a ref parameter. If you used a by
          value parameter, you could pass in derivedObject1 directly.



          Mattias

          --
          Mattias Sjögren [MVP] mattias @ mvps.org
          http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          Please reply only to the newsgroup.

          Comment

          • Richard Blewett [DevelopMentor]

            #6
            Re: Passing a Derived Object as a Reference Parameter

            Dispose doesn't clean up the same resources as the GC. Its there to allow timely clean up of *non-memory* resources. The GC only cleans up memory. If an object implements IDisposable and you have ownership of the object then you should call dispose on it. You are confusing GC with finalization. Finalization is something that happens (hopefully) after the GC realizes a finalizable object is collectable.I say hopefully because it isn't guaranteed to run. You need to make your object finalizable (implement a C# destructor) if and only if you are managing non-managed resrouces like windows HANDLEs and the like. If you implement a finalizer then you should also implement IDisposable. However, there are good reasons to implement IDisposable even if you don;t imeplement a finalizere (for example you have memebers which themselves implement IDisposable).

            Regards

            Richard Blewett - DevelopMentor



            "Bruce" <bvanderw-news5021@mailbl ocks.com > a ?crit dans le message de news:
            lr4b419mcdou87v 1hq9a495isurs92 brf7@4ax.com...
            [color=blue]
            > Something like this:
            >
            > void DestroyObject(r ef BaseObject obj)
            > {
            > obj.Dispose();
            > obj = null;
            > }
            >
            > DestroyObject(r ef derivedObject1) ;
            > DestroyObject(r ef derivedObject2) ;
            > DestroyObject(r ef derivedObject3) ;[/color]

            Yes, but I have to ask why you are explicitly disposing of objects instead
            of letting the garbage collector do its work ? Or do your classes hold
            references to Windows resources or file handles?

            Joanna


            Comment

            • Richard Blewett [DevelopMentor]

              #7
              Re: Passing a Derived Object as a Reference Parameter

              Dispose doesn't clean up the same resources as the GC. Its there to allow timely clean up of *non-memory* resources. The GC only cleans up memory. If an object implements IDisposable and you have ownership of the object then you should call dispose on it. You are confusing GC with finalization. Finalization is something that happens (hopefully) after the GC realizes a finalizable object is collectable.I say hopefully because it isn't guaranteed to run. You need to make your object finalizable (implement a C# destructor) if and only if you are managing non-managed resrouces like windows HANDLEs and the like. If you implement a finalizer then you should also implement IDisposable. However, there are good reasons to implement IDisposable even if you don;t imeplement a finalizere (for example you have memebers which themselves implement IDisposable).

              Regards

              Richard Blewett - DevelopMentor



              "Bruce" <bvanderw-news5021@mailbl ocks.com > a ?crit dans le message de news:
              lr4b419mcdou87v 1hq9a495isurs92 brf7@4ax.com...
              [color=blue]
              > Something like this:
              >
              > void DestroyObject(r ef BaseObject obj)
              > {
              > obj.Dispose();
              > obj = null;
              > }
              >
              > DestroyObject(r ef derivedObject1) ;
              > DestroyObject(r ef derivedObject2) ;
              > DestroyObject(r ef derivedObject3) ;[/color]

              Yes, but I have to ask why you are explicitly disposing of objects instead
              of letting the garbage collector do its work ? Or do your classes hold
              references to Windows resources or file handles?

              Joanna


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Passing a Derived Object as a Reference Parameter

                Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=blue][color=green]
                > >Where derivedObject1, 2 and 3 are of a class type derived from
                > >BaseObject?[/color]
                >
                > BaseObject bo = derivedObject1;
                > DestroyObject(r ef bo);
                >
                > But I don't understand why you use a ref parameter. If you used a by
                > value parameter, you could pass in derivedObject1 directly.[/color]

                I suspect the point is that the variable will be set to null
                afterwards. I can't say it's a pattern I really like though...

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Passing a Derived Object as a Reference Parameter

                  Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:[color=blue][color=green]
                  > >Where derivedObject1, 2 and 3 are of a class type derived from
                  > >BaseObject?[/color]
                  >
                  > BaseObject bo = derivedObject1;
                  > DestroyObject(r ef bo);
                  >
                  > But I don't understand why you use a ref parameter. If you used a by
                  > value parameter, you could pass in derivedObject1 directly.[/color]

                  I suspect the point is that the variable will be set to null
                  afterwards. I can't say it's a pattern I really like though...

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Joanna Carter \(TeamB\)

                    #10
                    Re: Passing a Derived Object as a Reference Parameter

                    "Richard Blewett [DevelopMentor]" <richardb@NOSPA Mdevelop.com> a écrit dans
                    le message de news: #R5#DglMFHA.314 4@TK2MSFTNGP10. phx.gbl...
                    [color=blue]
                    > Dispose doesn't clean up the same resources as the GC.[/color]

                    That was what I was trying to say in the last sentence of my post; you just
                    said it better :-))

                    Joanna

                    --
                    Joanna Carter
                    Consultant Software Engineer


                    Comment

                    Working...