4

I would like to declare a class Level DataSet in a Form code.

public partial class Frm_Stazione : Form
    {
        public Frm_Stazione()
        {
            InitializeComponent();
        }

    private readonly DataSet DS = new DataSet();

    private void Frm_Stazione_Load(object sender, EventArgs e)
    {
     ………
    }
  }

It is declared that way because the dataset must be accessible by different voids and must remain available until the Form is closed. My question is this: Visual Studio version 2019, indicates this error:

'IDE0069 The DS disposable field is never deleted'.

Certainly I'm wrong something, which can be my mistake. The code is written in C#.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Totonno
  • 73
  • 6

2 Answers2

3

Well, you have to Dispose DS instance since it's IDisposable (i.e. allocates some unmanaged resources which should be released via .Dispose()):

  public partial class MyForm : Form {
    ...
    // Unmanaged resources are allocated ...
    private readonly DataSet DS = new DataSet(); 
    ...

    protected override void Dispose(bool disposing) {
      base.Dispose(disposing);

      if (disposing) {
        // ... Unmanaged resources are released
        if (DS != null) { // <- check for null : in order to be on the safe side
          DS.Dispose();
        }
      }
    }
    ...
  } 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
3

For most of the components (not controls) when you drop an instance of the component on the form, the designer adds required code for disposal, but for DataSet , for some reason it doesn't add the dispose-related code.

But to be in safe side, if you want to dispose the data set, you can follow the same pattern which is used by the framework for all other component and it's defining a components container and adding all components to the container, then in the Dispose, dispose the container. This way, you will have a much cleaner code in dispose method.

To to so, in case the form doesn't have dispose-related code in designer.cs file, then you can easily add the following code to the form1.cs file:

private System.ComponentModel.IContainer components = new System.ComponentModel.Container();
protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

It will guarantee all the components will be disposed.

If the code exists in designer.cs just ignore above step. Then in your constructor just add the data set to the components container:

public Form1()
{
    InitializeComponent();
    components.Add(myDataSet);
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I know you already have an accepted answer, which is working and fine. But this answer also have some point, don't miss the points ;) – Reza Aghaei Feb 03 '20 at 09:03
  • Thank you for your time – Totonno Feb 04 '20 at 17:06
  • 1
    If you unaccept the other answer accidentally, feel free to keep the other post as accepted if you want, the decisions is yours :) JFYI, You can have only one accepted solution, but you can upvote as many solution that you find useful, including the accepted solution. Accepts and upvotes help the future readers to choose better solutions faster, they also are a token of appreciation for the answerers by adding reputations +10 for upvote and +15 for accept. – Reza Aghaei Feb 04 '20 at 17:10