Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Disposing a custom class

Disposing a custom class

Scheduled Pinned Locked Moved C#
question
13 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vodstok
    wrote on last edited by
    #1

    How do I do it? I created a class called "Category" that is essentially built from some strings and an int, and i need to be able to dispose of it when i am done (there are some issues arising from "used" objects not going away) I am inhereting the IDisposable interface, but i know that does nothing beyonf requiring you to ad a dispose method. any suggestions?

    ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

    C 1 Reply Last reply
    0
    • V Vodstok

      How do I do it? I created a class called "Category" that is essentially built from some strings and an int, and i need to be able to dispose of it when i am done (there are some issues arising from "used" objects not going away) I am inhereting the IDisposable interface, but i know that does nothing beyonf requiring you to ad a dispose method. any suggestions?

      ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Vodstok wrote:

      I created a class called "Category" that is essentially built from some strings and an int, and i need to be able to dispose of it when i am done

      There is nothing to dispose. You only need to use IDisposable if you are using unmanaged resources which you need to clean up, or you are using instances of classes that already implement IDisposable. Strings and ints fall in to neither category. The garbage collector will clean up the object when it is ready to do so.

      Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

      V 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Vodstok wrote:

        I created a class called "Category" that is essentially built from some strings and an int, and i need to be able to dispose of it when i am done

        There is nothing to dispose. You only need to use IDisposable if you are using unmanaged resources which you need to clean up, or you are using instances of classes that already implement IDisposable. Strings and ints fall in to neither category. The garbage collector will clean up the object when it is ready to do so.

        Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

        V Offline
        V Offline
        Vodstok
        wrote on last edited by
        #3

        ok, well that is a load off. This may be relevant to my problem: the object is being used as a static object. would that cause it to persist even though it has been "instantiated" with another identity? Essentially, the variable name is being reused on another page, but is returning all ofthe same information, even though it should be night and day different.

        ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

        C 1 Reply Last reply
        0
        • V Vodstok

          ok, well that is a load off. This may be relevant to my problem: the object is being used as a static object. would that cause it to persist even though it has been "instantiated" with another identity? Essentially, the variable name is being reused on another page, but is returning all ofthe same information, even though it should be night and day different.

          ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Vodstok wrote:

          the object is being used as a static object

          Do you mean that the class is declared as static:

          public static class MyClass
          {
          }

          Vodstok wrote:

          would that cause it to persist even though it has been "instantiated" with another identity?

          I don't understand what you mean by that. A static object, once created, will persist for the life of the application. You don't "instantiate" the object directly, the CLR decides when to do that. It is possible that a static class won't be "instantiated" (actually, for static classes, the term is "initialised") at all.

          Vodstok wrote:

          Essentially, the variable name is being reused on another page, but is returning all ofthe same information, even though it should be night and day different.

          If it is static then there is only ever the one "instance" (if you like).

          Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

          V 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Vodstok wrote:

            the object is being used as a static object

            Do you mean that the class is declared as static:

            public static class MyClass
            {
            }

            Vodstok wrote:

            would that cause it to persist even though it has been "instantiated" with another identity?

            I don't understand what you mean by that. A static object, once created, will persist for the life of the application. You don't "instantiate" the object directly, the CLR decides when to do that. It is possible that a static class won't be "instantiated" (actually, for static classes, the term is "initialised") at all.

            Vodstok wrote:

            Essentially, the variable name is being reused on another page, but is returning all ofthe same information, even though it should be night and day different.

            If it is static then there is only ever the one "instance" (if you like).

            Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

            V Offline
            V Offline
            Vodstok
            wrote on last edited by
            #5

            sort of. the class isnt static, but the instance is: public class Category { private static Category cat = null; public static Category Instance(int id) { if(cat == null) cat = new Category(); return cat; } Just an example snippet, but that is the gist of it

            ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

            C 1 Reply Last reply
            0
            • V Vodstok

              sort of. the class isnt static, but the instance is: public class Category { private static Category cat = null; public static Category Instance(int id) { if(cat == null) cat = new Category(); return cat; } Just an example snippet, but that is the gist of it

              ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              Vodstok wrote:

              the class isnt static, but the instance is

              Okay - You are using the singleton pattern (which ultimately should produce the same effect as a static class - except you have a lot more work to do) So, with that in mind, back to what you said earlier:

              Vodstok wrote:

              ok, well that is a load off. This may be relevant to my problem: the object is being used as a static object. would that cause it to persist even though it has been "instantiated" with another identity? Essentially, the variable name is being reused on another page, but is returning all of the same information, even though it should be night and day different.

              Assuming that the Instance method is the only thing that creates an Instance of the Category class (and that the constructor which isn't shown is private*) then it will get created once, and once only. The logic does not permit any other instance to be created. It doesn't matter where in your application you access this, you will always get the same instance back. * Which is normal for a singleton

              Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

              V 2 Replies Last reply
              0
              • C Colin Angus Mackay

                Vodstok wrote:

                the class isnt static, but the instance is

                Okay - You are using the singleton pattern (which ultimately should produce the same effect as a static class - except you have a lot more work to do) So, with that in mind, back to what you said earlier:

                Vodstok wrote:

                ok, well that is a load off. This may be relevant to my problem: the object is being used as a static object. would that cause it to persist even though it has been "instantiated" with another identity? Essentially, the variable name is being reused on another page, but is returning all of the same information, even though it should be night and day different.

                Assuming that the Instance method is the only thing that creates an Instance of the Category class (and that the constructor which isn't shown is private*) then it will get created once, and once only. The logic does not permit any other instance to be created. It doesn't matter where in your application you access this, you will always get the same instance back. * Which is normal for a singleton

                Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                V Offline
                V Offline
                Vodstok
                wrote on last edited by
                #7

                For starters, you answered a question i have had for a long time, whihc was "what the hell is this method of creating objects called", so THANK YOU, it has been eating at me since i was introduced to it last year. So let me see if i understand this properly. I use the method Category.Instance(1) to create an instance of an object, then go back and create Category.Instance(4), I now have 2 distinct objects that should not overlap in any way shape or form? I may have another unrelated problem, but as long as Ikno where not to loolk, i will be better off :) Again, Thank you :)

                ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

                C 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  Vodstok wrote:

                  the class isnt static, but the instance is

                  Okay - You are using the singleton pattern (which ultimately should produce the same effect as a static class - except you have a lot more work to do) So, with that in mind, back to what you said earlier:

                  Vodstok wrote:

                  ok, well that is a load off. This may be relevant to my problem: the object is being used as a static object. would that cause it to persist even though it has been "instantiated" with another identity? Essentially, the variable name is being reused on another page, but is returning all of the same information, even though it should be night and day different.

                  Assuming that the Instance method is the only thing that creates an Instance of the Category class (and that the constructor which isn't shown is private*) then it will get created once, and once only. The logic does not permit any other instance to be created. It doesn't matter where in your application you access this, you will always get the same instance back. * Which is normal for a singleton

                  Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                  V Offline
                  V Offline
                  Vodstok
                  wrote on last edited by
                  #8

                  I found out what my problem was.... i have the block that starts with if(cat == null), and all of the logic assigning values to the various strings and ints in the object were contained in that block, so when i feed it a new id, it does nothing with it, exactly the way i coded it.....

                  ______________________ Oh Hamburgers!

                  C 1 Reply Last reply
                  0
                  • V Vodstok

                    For starters, you answered a question i have had for a long time, whihc was "what the hell is this method of creating objects called", so THANK YOU, it has been eating at me since i was introduced to it last year. So let me see if i understand this properly. I use the method Category.Instance(1) to create an instance of an object, then go back and create Category.Instance(4), I now have 2 distinct objects that should not overlap in any way shape or form? I may have another unrelated problem, but as long as Ikno where not to loolk, i will be better off :) Again, Thank you :)

                    ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    Vodstok wrote:

                    I use the method Category.Instance(1) to create an instance of an object, then go back and create Category.Instance(4), I now have 2 distinct objects that should not overlap in any way shape or form?

                    No, you still only have one object. I've annotated your code below:

                    public static Category Instance(int id)
                    {
                    // Only create a new Category when there isn't one already
                    if (cat == null)
                    cat = new Category();

                    return cat;
                    

                    }

                    The logic prevents the creation of a new instance. cat is a static field, which means there will only be one regardless of how ever many instances of the class you create. If you want to create new distinct objects through a method you may want to look at the Factory Pattern instead - the code above is part of the Singleton Pattern. (Are you familiar with design patterns? Or have you just muddled through and stumbled upon them without realising? - Both scenarios are good in different ways). The Factory Pattern is a design pattern that permits a method (or class) to be responsible for the creation of objects. It can be a lot more flexible than using basic constructors. The Singleton Patterns is a design pattern that constrains a class so that there will only ever be one instance of the class. In C# this is more commonly achieved by using a static class (i.e. the pattern is built in to the language). In other languages you can do this by controlling all instantiation through a single method (this is what you are doing in your code)

                    Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                    1 Reply Last reply
                    0
                    • V Vodstok

                      I found out what my problem was.... i have the block that starts with if(cat == null), and all of the logic assigning values to the various strings and ints in the object were contained in that block, so when i feed it a new id, it does nothing with it, exactly the way i coded it.....

                      ______________________ Oh Hamburgers!

                      C Offline
                      C Offline
                      Colin Angus Mackay
                      wrote on last edited by
                      #10

                      Vodstok wrote:

                      I found out what my problem was....

                      This is what happens when I start writing a reply, get distracted, come back and finish it off... You find the answer by yourself. :-D

                      Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                      V 1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        Vodstok wrote:

                        I found out what my problem was....

                        This is what happens when I start writing a reply, get distracted, come back and finish it off... You find the answer by yourself. :-D

                        Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                        V Offline
                        V Offline
                        Vodstok
                        wrote on last edited by
                        #11

                        I appreciate you taking the time, and to answer the question in your other post, I have been muddling around in this pattern with no clear understanding of how it worked, but just that it made my life way easier. I have not ever looked at desing patterns.

                        ______________________ Oh Hamburgers!

                        C 1 Reply Last reply
                        0
                        • V Vodstok

                          I appreciate you taking the time, and to answer the question in your other post, I have been muddling around in this pattern with no clear understanding of how it worked, but just that it made my life way easier. I have not ever looked at desing patterns.

                          ______________________ Oh Hamburgers!

                          C Offline
                          C Offline
                          Colin Angus Mackay
                          wrote on last edited by
                          #12

                          Vodstok wrote:

                          I have not ever looked at desing patterns.

                          You might want to look out for a book on the subject. There are books on design patterns in C#, or there are generic books that apply to any language. It would seem to me that you have figured out, or almost figured out, some on your own. Reading up on the subject will help get you a better understanding, and you can short circuit some of the trial and error of getting the thing right in the first place. It will also give you some good ideas of how to proceed in areas where perhaps you are still figuring things our, or have a solution that just does not feel right. Once you have studied the basic patterns you can migrate on to some of the more interesting (in my opinion) patterns as given in Martin Fowler's book Patterns of Enterprise Application Architecture (IIRC). I wouldn't jump in to that book straight away though, it's a reference book for the most part, you'd need to understand the basics to get the best out of it. Good luck.

                          Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                          V 1 Reply Last reply
                          0
                          • C Colin Angus Mackay

                            Vodstok wrote:

                            I have not ever looked at desing patterns.

                            You might want to look out for a book on the subject. There are books on design patterns in C#, or there are generic books that apply to any language. It would seem to me that you have figured out, or almost figured out, some on your own. Reading up on the subject will help get you a better understanding, and you can short circuit some of the trial and error of getting the thing right in the first place. It will also give you some good ideas of how to proceed in areas where perhaps you are still figuring things our, or have a solution that just does not feel right. Once you have studied the basic patterns you can migrate on to some of the more interesting (in my opinion) patterns as given in Martin Fowler's book Patterns of Enterprise Application Architecture (IIRC). I wouldn't jump in to that book straight away though, it's a reference book for the most part, you'd need to understand the basics to get the best out of it. Good luck.

                            Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                            V Offline
                            V Offline
                            Vodstok
                            wrote on last edited by
                            #13

                            Thank you again, it looks like i have some reading to do :)

                            ______________________ Oh Hamburgers!

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups