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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Setting the DataTable object to Nothing

Setting the DataTable object to Nothing

Scheduled Pinned Locked Moved Visual Basic
csharpvisual-studiocomperformancetutorial
13 Posts 5 Posters 3 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.
  • C Offline
    C Offline
    Coding123456
    wrote on last edited by
    #1

    Hi All, I am interested to hear the pros and cons of two approaches and reusing a DataTable object or perhaps someone has a better idea. I have a DataTable object that I reuse. By this I mean I load it with data (using da.fill(dt)) use the data in the program, I then reload it with different data and use this new data. It does not go out of scope. Sometimes I don't want it to hold data as there is none to hold so I assign the DataTable to nothing (dt = Nothing). I do this so I can use the condition if dt is nothing then ... Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope, so when I load it with data again it holds the new values. This all works fine but in the microsoft documentation it has a phrase that makes me wary using this approach. It states "For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object." The full reference is here : http://msdn.microsoft.com/en-us/library/7cx5cffd(VS.71).aspx[^] When I assign new data to the object by using the dataadapter fill command I suppose I am just assigning a reference to the memory that holds the data so I don't know if the above really applies. For a simple example without all the fluff, consider; dim dt as new Datatable da.fill(dt) (other code) dt = nothing (other code) da.fill(dt) (other code) I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again. My alternative to the above if it is really bad is instead of setting it to nothing to use the dt.reset method to clear any data out of the datatable for example; dim dt as new Datatable da.fill(dt) (other code) dt.Reset (other code) da.fill(dt) I can then use, if dt.tables.count=0 orelse dt.tables(0).rows.count=0 then ..... to test for no data elsewhere in the code. I am assuming that reset clears out all data and when microsoft say 'Resets the system.data.datatable to its original state' I assume the original state is the state it was at when it was created? Any Thoughts on this would be appreciated

    C L D C 4 Replies Last reply
    0
    • C Coding123456

      Hi All, I am interested to hear the pros and cons of two approaches and reusing a DataTable object or perhaps someone has a better idea. I have a DataTable object that I reuse. By this I mean I load it with data (using da.fill(dt)) use the data in the program, I then reload it with different data and use this new data. It does not go out of scope. Sometimes I don't want it to hold data as there is none to hold so I assign the DataTable to nothing (dt = Nothing). I do this so I can use the condition if dt is nothing then ... Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope, so when I load it with data again it holds the new values. This all works fine but in the microsoft documentation it has a phrase that makes me wary using this approach. It states "For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object." The full reference is here : http://msdn.microsoft.com/en-us/library/7cx5cffd(VS.71).aspx[^] When I assign new data to the object by using the dataadapter fill command I suppose I am just assigning a reference to the memory that holds the data so I don't know if the above really applies. For a simple example without all the fluff, consider; dim dt as new Datatable da.fill(dt) (other code) dt = nothing (other code) da.fill(dt) (other code) I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again. My alternative to the above if it is really bad is instead of setting it to nothing to use the dt.reset method to clear any data out of the datatable for example; dim dt as new Datatable da.fill(dt) (other code) dt.Reset (other code) da.fill(dt) I can then use, if dt.tables.count=0 orelse dt.tables(0).rows.count=0 then ..... to test for no data elsewhere in the code. I am assuming that reset clears out all data and when microsoft say 'Resets the system.data.datatable to its original state' I assume the original state is the state it was at when it was created? Any Thoughts on this would be appreciated

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Setting it to nothing is a waste of time. If you want to free memory, call the Dispose method THEN set it to nothing, to mark it as disposed, and certainly create a new object before reusing it.

      Christian Graus Driven to the arms of OSX by Vista.

      C 1 Reply Last reply
      0
      • C Coding123456

        Hi All, I am interested to hear the pros and cons of two approaches and reusing a DataTable object or perhaps someone has a better idea. I have a DataTable object that I reuse. By this I mean I load it with data (using da.fill(dt)) use the data in the program, I then reload it with different data and use this new data. It does not go out of scope. Sometimes I don't want it to hold data as there is none to hold so I assign the DataTable to nothing (dt = Nothing). I do this so I can use the condition if dt is nothing then ... Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope, so when I load it with data again it holds the new values. This all works fine but in the microsoft documentation it has a phrase that makes me wary using this approach. It states "For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object." The full reference is here : http://msdn.microsoft.com/en-us/library/7cx5cffd(VS.71).aspx[^] When I assign new data to the object by using the dataadapter fill command I suppose I am just assigning a reference to the memory that holds the data so I don't know if the above really applies. For a simple example without all the fluff, consider; dim dt as new Datatable da.fill(dt) (other code) dt = nothing (other code) da.fill(dt) (other code) I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again. My alternative to the above if it is really bad is instead of setting it to nothing to use the dt.reset method to clear any data out of the datatable for example; dim dt as new Datatable da.fill(dt) (other code) dt.Reset (other code) da.fill(dt) I can then use, if dt.tables.count=0 orelse dt.tables(0).rows.count=0 then ..... to test for no data elsewhere in the code. I am assuming that reset clears out all data and when microsoft say 'Resets the system.data.datatable to its original state' I assume the original state is the state it was at when it was created? Any Thoughts on this would be appreciated

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        IMHO that MSDN page is completely wrong. And I am sure Christian's advice is sound. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Fixturized forever. :confused:


        1 Reply Last reply
        0
        • C Coding123456

          Hi All, I am interested to hear the pros and cons of two approaches and reusing a DataTable object or perhaps someone has a better idea. I have a DataTable object that I reuse. By this I mean I load it with data (using da.fill(dt)) use the data in the program, I then reload it with different data and use this new data. It does not go out of scope. Sometimes I don't want it to hold data as there is none to hold so I assign the DataTable to nothing (dt = Nothing). I do this so I can use the condition if dt is nothing then ... Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope, so when I load it with data again it holds the new values. This all works fine but in the microsoft documentation it has a phrase that makes me wary using this approach. It states "For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object." The full reference is here : http://msdn.microsoft.com/en-us/library/7cx5cffd(VS.71).aspx[^] When I assign new data to the object by using the dataadapter fill command I suppose I am just assigning a reference to the memory that holds the data so I don't know if the above really applies. For a simple example without all the fluff, consider; dim dt as new Datatable da.fill(dt) (other code) dt = nothing (other code) da.fill(dt) (other code) I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again. My alternative to the above if it is really bad is instead of setting it to nothing to use the dt.reset method to clear any data out of the datatable for example; dim dt as new Datatable da.fill(dt) (other code) dt.Reset (other code) da.fill(dt) I can then use, if dt.tables.count=0 orelse dt.tables(0).rows.count=0 then ..... to test for no data elsewhere in the code. I am assuming that reset clears out all data and when microsoft say 'Resets the system.data.datatable to its original state' I assume the original state is the state it was at when it was created? Any Thoughts on this would be appreciated

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          If your just dumping the old data, just create a new DataTable object and forget about worrying about this. The CLR GC will handle the situation appropriately and do cleanup when required.

          dim dt as new Datatable
          da.fill(dt)
          (other code)
          dt.Dispose()
          dt = New DataTable

          (other code)
          da.fill(dt)

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          modified on Thursday, December 11, 2008 10:54 AM

          L C 2 Replies Last reply
          0
          • C Coding123456

            Hi All, I am interested to hear the pros and cons of two approaches and reusing a DataTable object or perhaps someone has a better idea. I have a DataTable object that I reuse. By this I mean I load it with data (using da.fill(dt)) use the data in the program, I then reload it with different data and use this new data. It does not go out of scope. Sometimes I don't want it to hold data as there is none to hold so I assign the DataTable to nothing (dt = Nothing). I do this so I can use the condition if dt is nothing then ... Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope, so when I load it with data again it holds the new values. This all works fine but in the microsoft documentation it has a phrase that makes me wary using this approach. It states "For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object." The full reference is here : http://msdn.microsoft.com/en-us/library/7cx5cffd(VS.71).aspx[^] When I assign new data to the object by using the dataadapter fill command I suppose I am just assigning a reference to the memory that holds the data so I don't know if the above really applies. For a simple example without all the fluff, consider; dim dt as new Datatable da.fill(dt) (other code) dt = nothing (other code) da.fill(dt) (other code) I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again. My alternative to the above if it is really bad is instead of setting it to nothing to use the dt.reset method to clear any data out of the datatable for example; dim dt as new Datatable da.fill(dt) (other code) dt.Reset (other code) da.fill(dt) I can then use, if dt.tables.count=0 orelse dt.tables(0).rows.count=0 then ..... to test for no data elsewhere in the code. I am assuming that reset clears out all data and when microsoft say 'Resets the system.data.datatable to its original state' I assume the original state is the state it was at when it was created? Any Thoughts on this would be appreciated

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

            Coding123456 wrote:

            Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope

            Correction: The garbage collector will not destroy the object until all references to it are unreachable.

            Coding123456 wrote:

            For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object

            I have no idea what the name property on an object has to do with anything, unless there is some pooling going on somewhere which is keyed on the name property. If there was it should be well documented. Setting the reference to Nothing makes it unreachable (so long as there are no other references to it.) If the object implements the IDisposable interface then you should, as a matter of good practice, call Dispose on the object.

            Coding123456 wrote:

            I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again.

            I don't see a problem with that. If you set it to nothing and there is a consirerable amount of time before assigning new data then do so because in the intervening time the Garbage Collector can get it. If it is the very next statement where you assign data I don't really see much point.

            Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

            C 1 Reply Last reply
            0
            • D Dave Kreskowiak

              If your just dumping the old data, just create a new DataTable object and forget about worrying about this. The CLR GC will handle the situation appropriately and do cleanup when required.

              dim dt as new Datatable
              da.fill(dt)
              (other code)
              dt.Dispose()
              dt = New DataTable

              (other code)
              da.fill(dt)

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              modified on Thursday, December 11, 2008 10:54 AM

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I would suggest you dispose of the first DataTable though. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Fixturized forever. :confused:


              D 1 Reply Last reply
              0
              • L Luc Pattyn

                I would suggest you dispose of the first DataTable though. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Fixturized forever. :confused:


                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Whoops! I meant to put that in there... Strike 1! :-\

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                1 Reply Last reply
                0
                • C Christian Graus

                  Setting it to nothing is a waste of time. If you want to free memory, call the Dispose method THEN set it to nothing, to mark it as disposed, and certainly create a new object before reusing it.

                  Christian Graus Driven to the arms of OSX by Vista.

                  C Offline
                  C Offline
                  Coding123456
                  wrote on last edited by
                  #8

                  Hi Christian, I'm not trying to free memory. I'm setting the Datatable to nothing so I can test for it being nothing later in the code so I can control program flow based on if I loaded it with data or not. I agree with your suggestion though if I wanted to free memory.

                  1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    If your just dumping the old data, just create a new DataTable object and forget about worrying about this. The CLR GC will handle the situation appropriately and do cleanup when required.

                    dim dt as new Datatable
                    da.fill(dt)
                    (other code)
                    dt.Dispose()
                    dt = New DataTable

                    (other code)
                    da.fill(dt)

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    modified on Thursday, December 11, 2008 10:54 AM

                    C Offline
                    C Offline
                    Coding123456
                    wrote on last edited by
                    #9

                    Thanks for your suggestion Dave and your right I am dumping old data but also setting the datatable to a state where I can test it and control program flow. I did't go into too much detail in my original post but the Datatable is inside a class that stays around while the program is executing. I use various properties of the class to retreive and manipulate the data in the datatables (more than one) inside the class. As the datatables stay around for the life of the class and therefore the program, I can't really just create a new one as my properties need to reference the same datatable (which contains data or I set to Nothing indicating no data) to make sense of the data and the program needs to reference the same class. If the datatable happens to be disposed (as it contained no data) as in your example I would get an error when I reference it.

                    D 1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      Coding123456 wrote:

                      Using .NET apparently the garbage collector does not destroy the object as long as I don't go out of scope

                      Correction: The garbage collector will not destroy the object until all references to it are unreachable.

                      Coding123456 wrote:

                      For example, if an object is set to Nothing inside a procedure and the next line of code creates an object of the same name, the first object may not yet be destroyed and a reference to the new object might incorrectly return the first object

                      I have no idea what the name property on an object has to do with anything, unless there is some pooling going on somewhere which is keyed on the name property. If there was it should be well documented. Setting the reference to Nothing makes it unreachable (so long as there are no other references to it.) If the object implements the IDisposable interface then you should, as a matter of good practice, call Dispose on the object.

                      Coding123456 wrote:

                      I'm not entirly comfortable with this approach as it seems bad to set it to nothing if I actually intend to reuse it again and again.

                      I don't see a problem with that. If you set it to nothing and there is a consirerable amount of time before assigning new data then do so because in the intervening time the Garbage Collector can get it. If it is the very next statement where you assign data I don't really see much point.

                      Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

                      C Offline
                      C Offline
                      Coding123456
                      wrote on last edited by
                      #10

                      Hi Colin, you say that

                      Colin Angus Mackay wrote:

                      Setting the reference to Nothing makes it unreachable (so long as there are no other references to it.) If the object implements the IDisposable interface then you should, as a matter of good practice, call Dispose on the object.

                      When I set the datatable to Nothing I am making the data unreachable which is what I want to do and it also gives me a condition I can test for to control program flow i.e. if dt is nothing then... I don't want to dispose it as I may want to do something like this;

                      If dt is nothing then
                      return "Default Name"
                      else
                      return cstr(dt.rows(0).item("Name"))
                      end if

                      Colin Angus Mackay wrote:

                      I don't see a problem with that. If you set it to nothing and there is a consirerable amount of time before assigning new data then do so because in the intervening time the Garbage Collector can get it. If it is the very next statement where you assign data I don't really see much point.

                      So you are saying when I set the dt=nothing the data it pointed to will get cleaned up and dt still hangs around safely for me to reuse. This is reasssuring. The time to assign new data may be immediate or after some time. It is really not the very next statement I have just simplified the code. Thanks for your reply

                      C 1 Reply Last reply
                      0
                      • C Coding123456

                        Hi Colin, you say that

                        Colin Angus Mackay wrote:

                        Setting the reference to Nothing makes it unreachable (so long as there are no other references to it.) If the object implements the IDisposable interface then you should, as a matter of good practice, call Dispose on the object.

                        When I set the datatable to Nothing I am making the data unreachable which is what I want to do and it also gives me a condition I can test for to control program flow i.e. if dt is nothing then... I don't want to dispose it as I may want to do something like this;

                        If dt is nothing then
                        return "Default Name"
                        else
                        return cstr(dt.rows(0).item("Name"))
                        end if

                        Colin Angus Mackay wrote:

                        I don't see a problem with that. If you set it to nothing and there is a consirerable amount of time before assigning new data then do so because in the intervening time the Garbage Collector can get it. If it is the very next statement where you assign data I don't really see much point.

                        So you are saying when I set the dt=nothing the data it pointed to will get cleaned up and dt still hangs around safely for me to reuse. This is reasssuring. The time to assign new data may be immediate or after some time. It is really not the very next statement I have just simplified the code. Thanks for your reply

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

                        Coding123456 wrote:

                        I don't want to dispose it as I may want to do something like this

                        Eh?! You've already made it unreachable, you can't dispose something that is unreachable, you have to wait until the Garbage Collector does it for you. I think you seem to misunderstand what the Dispose method is doing. Some classes implement the IDisposable interface which gives you a Dispose method. If a class gives you a Dispose method then you MUST as a matter of good practice dispose the object BEFORE making it unreachable. If you don't then the garbage collector will have to clean up the additional mess for you. If a class implements Dispose it is effectively saying "I make a bit of a mess, but I can clean up after myself". You do not want the Garbage Collector calling Dispose for you. Ever!

                        Coding123456 wrote:

                        So you are saying when I set the dt=nothing the data it pointed to will get cleaned up and dt still hangs around safely for me to reuse

                        dt is a reference to something. The data that was referenced to it will be cleaned up by the Garbage Collector when there are no more references to it (i.e. it becomes unreachable). You can safely assign another object to dt and it will refer to the new object.

                        Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

                        1 Reply Last reply
                        0
                        • C Coding123456

                          Thanks for your suggestion Dave and your right I am dumping old data but also setting the datatable to a state where I can test it and control program flow. I did't go into too much detail in my original post but the Datatable is inside a class that stays around while the program is executing. I use various properties of the class to retreive and manipulate the data in the datatables (more than one) inside the class. As the datatables stay around for the life of the class and therefore the program, I can't really just create a new one as my properties need to reference the same datatable (which contains data or I set to Nothing indicating no data) to make sense of the data and the program needs to reference the same class. If the datatable happens to be disposed (as it contained no data) as in your example I would get an error when I reference it.

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          I think you have left out some things in your description. Setting the datatable reference to Nothing destroys the table, colums, data, everything. You cannot reuse that object. Now you're saying you WANT to reuse the datatable and its columns, but no data. Which is it??

                          Coding123456 wrote:

                          my properties need to reference the same datatable (which contains data or I set to Nothing indicating no data) to make sense of the data and the program needs to reference the same class.

                          Coding123456 wrote:

                          If the datatable happens to be disposed (as it contained no data) as in your example I would get an error when I reference it.

                          These two things are mutually exclusive. You cannot have it both ways. I think you need to rethink this data model.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007, 2008

                          C 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            I think you have left out some things in your description. Setting the datatable reference to Nothing destroys the table, colums, data, everything. You cannot reuse that object. Now you're saying you WANT to reuse the datatable and its columns, but no data. Which is it??

                            Coding123456 wrote:

                            my properties need to reference the same datatable (which contains data or I set to Nothing indicating no data) to make sense of the data and the program needs to reference the same class.

                            Coding123456 wrote:

                            If the datatable happens to be disposed (as it contained no data) as in your example I would get an error when I reference it.

                            These two things are mutually exclusive. You cannot have it both ways. I think you need to rethink this data model.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                 2006, 2007, 2008

                            C Offline
                            C Offline
                            Coding123456
                            wrote on last edited by
                            #13

                            Dave Kreskowiak wrote:

                            I think you have left out some things in your description. Setting the datatable reference to Nothing destroys the table, colums, data, everything. You cannot reuse that object. Now you're saying you WANT to reuse the datatable and its columns, but no data. Which is it??

                            Setting the datatable to Nothing I agree it will destroy the table. Which is fine as I don't want to reuse it's columns or data. I just want to be able to test if the datatable is Nothing and if so run some code. Or sometime later when if it does contain data, run different code. To put it a different way, if dt is nothing I know there is no data associated with it, otherwise I can read data from the datatable. I am using its 'nothingness' as a flag Thanks for your comments to date I appreciate your interest. I realise that I may use one word that inadvertently changes the the exact meaning of what I am trying to describe.

                            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