I’ve been working on a project that uses table adapters and datasets to access an access database. I’m near completion and ran code analysis from visual studio and it came up with some errors about needing to implement IDisposable on specific classes that I use these methods. While I’ve looked at a few different things on this about having a dispose method or using a using block, I’m not really sure how to make this work. The dataset and table adapters are created as global variables to be used by the whole class, many classes will call on other classes which will also use different table adapters and datasets. I’ve tried creating a Dispose method, but I don’t know when to call it and I am wondering if it is called at the wrong time will it crash my program. Here is a sample of one of the classes that I need to implement Idisposable :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace recipeDataBase
{
class NewRecipe : IDisposable
{
recipiesNewDataSet recipeDataSet = new recipiesNewDataSet();
recipiesNewDataSetTableAdapters.RecipeTableAdapter recipeTableAdapter = new recipiesNewDataSetTableAdapters.RecipeTableAdapter();
recipiesNewDataSetTableAdapters.RecipeIngredientTableAdapter recipeIngredientTableAdapter = new recipiesNewDataSetTableAdapters.RecipeIngredientTableAdapter();
recipiesNewDataSetTableAdapters.RatingTableAdapter ratingTableAdapter = new recipiesNewDataSetTableAdapters.RatingTableAdapter();
recipeDataBase.recipiesNewDataSetTableAdapters.IngredientTableAdapter ingredientTableAdapter = new recipiesNewDataSetTableAdapters.IngredientTableAdapter();
private RecipeInfo newRecipe;
private RatingNum originalRatingNum;
private RatingNum newRating;
private RecipeInfo originalRecipe;
private string[] ingredients;
public NewRecipe(RecipeInfo incommingNewRecipe, RatingNum IncommingNewRating, string[] incommingIngredients)
{
newRecipe = incommingNewRecipe;
newRating = IncommingNewRating;
ingredients = incommingIngredients;
CreateNewRecipe();
UpdateNewRecipe();
}
public void CreateNewRecipe()
{
originalRatingNum = new RatingNum();
originalRecipe = new RecipeInfo();
originalRatingNum.cookingTime = 0;
originalRatingNum.easeOfCooking = 0;
originalRatingNum.familyRating = 0;
originalRatingNum.healthRating = 0;
originalRatingNum.userRating = 0;
ratingTableAdapter.Fill(recipeDataSet.Rating);
ratingTableAdapter.Insert(originalRatingNum.userRating, originalRatingNum.familyRating, originalRatingNum.healthRating, originalRatingNum.easeOfCooking, originalRatingNum.cookingTime);
Query getNewRecipeNumbers = new Query();
int newRatingNumber = getNewRecipeNumbers.newRatingNum();
originalRatingNum.ratingNum = newRatingNumber;
newRating.ratingNum = newRatingNumber;
newRecipe.ratingNum = newRatingNumber;
originalRecipe.recipeName = "newRecipe";
originalRecipe.nationality = "newRecipe";
originalRecipe.recipeEvent = "newRecipe";
originalRecipe.source = "newRecipe";
originalRecipe.type = "newRecipe";
originalRecipe.servings = "0";
originalRecipe.ratingNum = newRatingNumber;
recipeTableAdapter.Fill(recipeDataSet.Recipe);
recipeTableAdapter.Insert(originalRecipe.recipeName, originalRecipe.nationality, originalRecipe.recipeEvent, originalRecipe.source, originalRecipe.type, originalRecipe.servings, originalRecipe.ratingNum);
int newRecipeNum = getNewRecipeNumbers.newRecipeNum();
newRecipe.recipeNum = newRecipeNum;
originalRecipe.recipeNum = newRecipeNum;
recipeDataSet.AcceptChanges();
}
public void UpdateNewRecipe()
{
UpdateRatingNum updateRatingNum = new UpdateRatingNum(originalRatingNum, newRating);
UpdateRecipe updateRecipe = new UpdateRecipe(newRecipe, originalRecipe);
UpdateIngredients updateIngredients = new UpdateIngredients(ingredients);
UpdateRecipeIngredient updateRecpeIngredients = new UpdateRecipeIngredient(ingredients, newRecipe.recipeNum);
recipeDataSet.AcceptChanges();
}
public void Dispose()
{
ratingTableAdapter.Dispose();
recipeTableAdapter.Dispose();
recipeTableAdapter.Dispose();
ingredientTableAdapter.Dispose();
recipeDataSet.Dispose();
throw new NotImplementedException();
}
}
}
As you can see I did implement Idisposable and used the automatically created method to put all of the table adapters and dataset in there to be disposed, but how do I use it and where?
Thanks for the help
Craig
As mentioned in other answers the correct way to usage an object that implements
IDisposableis withing a using block as follows:What I would like to point out though is the correct implementation of the IDisposable pattern. Note that there is a complete example on the MSDN article for the IDisposable interface.
Also note that you should NEVER throw an exception during finalization (IE:
~Recipe()). ConsequentlyDispose(bool)should NEVER throw an exception.