-
September 13th, 2013, 03:37 PM
#1
Destruktor does it need boodisposed
Hi, this is mario I try to figure out if that boodispose is really needed.
So far as I can see, it has only a value if I would have called '.dispose' two times for the same Class.
In that case both of them ClassA and ClassB would not call again their process to free their objects.
I dont see anymore reason.
Can anyone help me here?
Code:
using System.Data.SqlClient;
public class ClassA : IDisposable
{
private SqlConnection objCnnA;
private string mServerName;
private bool mbooDisposed = false;
public ClassA()
{
objCnnA = new SqlConnection();
}
protected virtual void dispose()
{
try {
this.dispose(true);
GC.SuppressFinalize(this);
} catch (Exception ex) {
MessageBox.Show("ClassA dispose =" + ex.Message);
}
}
void IDisposable.Dispose()
{
dispose();
}
protected virtual void Dispose(bool BoolDisposed)
{
try {
if (!(this.mbooDisposed)) {
if (BoolDisposed) {
if (objCnnA is IDisposable) {
(objCnnA as IDisposable).Dispose();
}
if ((objCnnA != null))
objCnnA = null;
//MyBase.dispose()
base.Finalize();
}
}
this.mbooDisposed = true;
} catch (Exception ex) {
MessageBox.Show("Basic Finalize =" + ex.Message);
}
}
public virtual string ServerName {
//overridable: vielleicht wird mehr als eine ClassB deklariert
get { return mServerName; }
set { mServerName = value; }
}
public virtual void OpenCnn()
{
objCnnA.ConnectionString = "integrated security=SSPI;data source=" + mServerName + ";" + "persist security info=true;initial catalog=DB_France";
objCnnA.Open();
}
public virtual void CloseCnn()
{
if (objCnnA.State == ConnectionState.Open) {
objCnnA.Close();
}
}
}
using System.Data.SqlClient;
public class ClassB : ClassA
{
private SqlConnection objCnnB;
private bool disposed = false;
public ClassB() : base()
{
objCnnB = new SqlConnection();
}
protected override void dispose()
{
try {
this.Dispose(true);
GC.SuppressFinalize(this);
} catch (Exception ex) {
MessageBox.Show("ClassB. dispose = " + ex.Message);
}
}
protected virtual void Dispose(bool boodisposed)
{
try {
if (!(this.disposed)) {
if (boodisposed) {
if (objCnnB is IDisposable) {
(objCnnB as IDisposable).Dispose();
}
if ((objCnnB != null))
objCnnB = null;
}
}
base.dispose(disposed);
this.disposed = true;
} catch (Exception ex) {
MessageBox.Show("ClassB Dispose Overloads =" + ex.Message);
}
}
public override string ServerName {
get { return base.ServerName; }
set { base.ServerName = value; }
}
public override void OpenCnn()
{
base.OpenCnn();
}
public override void CloseCnn()
{
base.CloseCnn();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|