Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day

This may be a stupid question but...
If you have a public datatable used for info between programs visual studio
Public Shared DtRptDisc As New DataTable("DtRptDisc")

If the case scenario is that you want to refresh the datatable and you do as follows
DtRptDisc = New Datatable

Is this creating a duplicate instance ? or recreating the same instance?
Meaning that the previous Datable is still resident in memory as was not disposed?
So meaning is this cycle repetitively done will this chew up resources? Does not seem to but unsure.

What I have tried:

Google Search, Have not disposed of the datatable in the App as currently or continuously in use so a global resource been used all the time.
Posted
Updated 25-Oct-21 1:30am

Quote:
VB.NET
DtRptDisc = New Datatable

Is this creating a duplicate instance ? or recreating the same instance?
It creates a new DataTable instance, and clears the reference to the old one from the field.

If you have used the value of that field anywhere else, there may be a reference to the original instance hanging around. If not, the GC will eventually clean up the memory used by the old instance.

If you want to ensure that only one instance is ever used, and that all references share the same view of the data, you should reload the DataTable rather than creating a new instance.
 
Share this answer
 
Comments
Grid-Code 29-Oct-21 9:05am    
Hi Richard

Much appreciate your input and understand the functionality, Reload a more viable method to ensure memory is not been chewed up in the backend of application.
If you want to make sure the DataTable is disposed, you can use a using clause in C# like this:
using (DataTable table = new DataTable())
{
  // your code
}

For the VB.NET syntax see: Using Statement - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Grid-Code 23-Oct-21 2:31am    
Hi RickZeeland

Appreciate the c# Snip
So New Datatable disposes the Prior one - Brilliant

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900