Below is my entire class that I am using, I have two questions, 1 is this the proper use of Dispose() and also, why am I getting the error No Overload for method ‘dispose’ takes 1 argument.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Crawler
{
class LoggingClass : IDisposable
{
public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery)
{
string Loggingall = " insert into tblLogs " +
"set SystemMsg='" + systemMsg.Replace("'","''") + "'" +
",SystemClass = '" + SystemClass.Replace("'", "''") + "'" +
",SystemSection = '" + SystemSection.Replace("'", "''") + "'" +
",ID = '" + CarID.Replace("'", "''") + "'" +
",FixID = '" + FixID.Replace("'", "''") + "'" +
",baseurl = '" + baseURL.Replace("'", "''") + "'" +
",mysqlqueryName = '" + mysqlQuery.Replace("'", "''") + "'" +
",mysqlquery = '" + mysqlQuery.Replace("'", "''") + "'" +
",TimeStamp = Now()";
MySQLProcessing.MySQLProcessor MYSQLP = new MySQLProcessing.MySQLProcessor();
MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");
}
public void Dispose()
{
Dispose(true);
// Take yourself off the Finalization queue
// to prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
}
}
Here is my updated code:
}
Is this the correct way to call it? Do i also have to call the dispose?
As other answerers have mentioned, it doesn’t look like you need to implement IDisposable. You have no class fields at all, so there’s nothing to clean up.
Assuming there’s more to the class than you’ve shown, you’re following the pattern for implementing IDisposable, but you’re only halfway done.
The pattern is to have
IDisposable.Dispose()and the finalizer (~LoggingClass) both call a common method,Dispose(bool). In theDispose(bool)method, you should clean up both managed and unmanaged resources if the boolean is passed true, and only clean up the unmanaged resources if passed false.Here’s the code I use for implementing IDisposable.
Edit
It looks like you added your updated code, and then removed it. But, here’s my comments on how you’re calling it.
With the GenericLogging method as you currently have it, you don’t need IDisposable at all. However, there are a couple things I would do to improve up your code.
constructor, rather than in the GenericLogging method.
whatever that class has) in the managed cleanup section of
Dispose(bool).
demonstrated is a properly implemented
usingstatement, but you’llend up creating & destroying thousands of objects in your code.
Create one LoggingClass object, and keep it around for the entire
length of the program, not just for one log statement.
usingstatement).