C
Constantine
Hi, I have developed one class called CProductInfo providing == and !=
operator features. I have one problem.
When I use this class in my program, say
CProductInfo product = null;
and later, when i check for emptiness,
if (product==null)
it returns false as if the product instance is not empty.
I tried various ways to eliminate this run-time error, but it just does
not happen. Also I don't think there is use full tips on net for this.
Can someone help?
Thanks in advance - Rhonald
<<<< THE SOURCE CODE FOR CProductInfo IS BELOW >>>>>
using System;
using System.Data;
using System.Data.SqlClient;
using MyDataLibrary;
namespace ProductManager2006
{
/// <summary>
/// Summary description for CProductInfo.
/// </summary>
public class CProductInfo:ConnectionManager
{
internal long m_AutoIndex = 0;
internal string m_SerialNumber = "";
public string Title = "";
private DateTime m_DateReference = DateTime.Now;
private DateTime m_DateStart = DateTime.Now, m_DateFinish =
DateTime.Now;
public ProductType Type = ProductType.CustomizedProduct;
public string Version = "", Build = "", Explanation = "";
public string Notes = "", Features = "", SystemRequirements = "",
PreRequisites = "";
public bool Status = true;
private bool m_IsDateStartAvailable = false, m_IsDateFinishAvailable
= false;
public CProductInfo()
{
//
// TODO: Add constructor logic here
//
}
public CProductInfo(string productId)
{
Open(productId);
}
public CProductInfo(DataRow dr)
{
if (dr!=null)
{
m_AutoIndex = DataConverter.ToLong(dr["auto_index"].ToString());
m_SerialNumber = dr["pk_product_id"].ToString();
m_DateReference =
Convert.ToDateTime(dr["date_reference"].ToString());
Title = dr["title"].ToString();
if (!dr.IsNull(4)) DateStart =
Convert.ToDateTime(dr["date_start"].ToString());
if (!dr.IsNull(5)) DateFinish =
Convert.ToDateTime(dr["date_finish"].ToString());
Type =
DataConverter.ToBoolean(dr["product_type"].ToString())?ProductType.GeneralizedProductroductType.CustomizedProduct;
Version = dr["version"].ToString();
Build = dr["build"].ToString();
Notes = dr["notes"].ToString();
Features = dr["features"].ToString();
SystemRequirements = dr["system_requirements"].ToString();
PreRequisites = dr["prerequisites"].ToString();
Status = DataConverter.ToBoolean(dr["status"].ToString());
}
}
private string _NewID
{
get
{
SqlCommand cmdSql = new SqlCommand("select isnull(max(auto_index),
0) as counter from mstrproducts", Connection);
try
{
OpenConnection();
SqlDataReader dr = cmdSql.ExecuteReader();
if (dr.Read())
{
return
((DataConverter.ToInteger(dr["counter"].ToString())+1).ToString("00000"));
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
finally
{
CloseConnection();
}
return null;
}
}
public string SerialNumber
{
get
{
return m_SerialNumber;
}
}
public DateTime DateReference
{
get
{
return m_DateReference;
}
}
public DateTime DateStart
{
get
{
return m_DateStart;
}
set
{
m_DateStart = value;
m_IsDateStartAvailable = true;
}
}
public DateTime DateFinish
{
get
{
return m_DateFinish;
}
set
{
m_DateFinish = value;
m_IsDateFinishAvailable = true;
}
}
public bool IsDateStartAvailable
{
get
{
return m_IsDateStartAvailable;
}
}
public bool IsDateFinishAvailable
{
get
{
return m_IsDateFinishAvailable;
}
}
public bool IsCustomBuilt
{
get
{
return Type == ProductType.CustomizedProduct?true:false;
}
}
public bool IsGeneralBuilt
{
get
{
return Type == ProductType.GeneralizedProduct?true:false;
}
}
public bool Open(string id)
{
SqlCommand cmdSql = new SqlCommand("select * from mstrproducts where
pk_product_id='"+id+"'", Connection);
try
{
OpenConnection();
SqlDataReader dr = cmdSql.ExecuteReader();
if (dr.Read())
{
m_AutoIndex = DataConverter.ToLong(dr["auto_index"].ToString());
m_SerialNumber = dr["pk_product_id"].ToString();
m_DateReference =
Convert.ToDateTime(dr["date_reference"].ToString());
Title = dr["title"].ToString();
if (!dr.IsDBNull(4))
{
DateStart = Convert.ToDateTime(dr["date_start"].ToString());
m_IsDateStartAvailable = true;
}
else
m_IsDateStartAvailable = false;
if (!dr.IsDBNull(5))
{
DateFinish = Convert.ToDateTime(dr["date_finish"].ToString());
m_IsDateFinishAvailable = true;
}
else
m_IsDateFinishAvailable = false;
Type =
DataConverter.ToBoolean(dr["product_type"].ToString())?ProductType.GeneralizedProductroductType.CustomizedProduct;
Version = dr["version"].ToString();
Build = dr["build"].ToString();
Notes = dr["notes"].ToString();
Features = dr["features"].ToString();
SystemRequirements = dr["system_requirements"].ToString();
PreRequisites = dr["prerequisites"].ToString();
Status = DataConverter.ToBoolean(dr["status"].ToString());
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
finally
{
CloseConnection();
}
return true;
}
internal bool Save()
{
SqlCommand cmdSql = new SqlCommand("RegisterProducts", Connection);
cmdSql.CommandType = CommandType.StoredProcedure;
if (m_SerialNumber.Trim().Length<=0) m_SerialNumber = _NewID;
cmdSql.Parameters.Add(new SqlParameter("@product_id",
SqlDbType.VarChar, 10)).Value = SerialNumber;
cmdSql.Parameters.Add(new SqlParameter("@date_reference",
SqlDbType.SmallDateTime)).Value =
Convert.ToDateTime(DateReference.ToShortDateString());
cmdSql.Parameters.Add(new SqlParameter("@title", SqlDbType.VarChar,
150)).Value = Title;
if (IsDateStartAvailable) cmdSql.Parameters.Add(new
SqlParameter("@date_start", SqlDbType.SmallDateTime)).Value =
DateStart;
if (IsDateFinishAvailable) cmdSql.Parameters.Add(new
SqlParameter("@date_finish", SqlDbType.SmallDateTime)).Value =
DateFinish;
cmdSql.Parameters.Add(new SqlParameter("@product_type",
SqlDbType.Bit)).Value = (Type == ProductType.GeneralizedProduct?0:1);
cmdSql.Parameters.Add(new SqlParameter("@version",
SqlDbType.VarChar, 15)).Value = Version;
cmdSql.Parameters.Add(new SqlParameter("@build", SqlDbType.VarChar,
15)).Value = Build;
cmdSql.Parameters.Add(new SqlParameter("@notes", SqlDbType.NVarChar,
500)).Value = Notes;
cmdSql.Parameters.Add(new SqlParameter("@features",
SqlDbType.NVarChar, 500)).Value = Features;
cmdSql.Parameters.Add(new SqlParameter("@system_requirements",
SqlDbType.NVarChar, 500)).Value = SystemRequirements;
cmdSql.Parameters.Add(new SqlParameter("@prerequisites",
SqlDbType.NVarChar, 500)).Value = PreRequisites;
cmdSql.Parameters.Add(new SqlParameter("@status",
SqlDbType.Bit)).Value = (Status?1:0);
try
{
OpenConnection();
cmdSql.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
return true;
}
internal bool Delete()
{
return false;
}
public static bool operator == (CProductInfo product1, CProductInfo
product2)
{
return product1.Equals(product2);
}
public static bool operator != (CProductInfo product1, CProductInfo
product2)
{
return (!product1.Equals(product2));
}
public override bool Equals(object obj)
{
if (!(obj is CProductInfo)) return false;
return this==(CProductInfo)obj;
}
public override int GetHashCode()
{
return base.GetHashCode ();
}
public override string ToString()
{
return base.ToString ();
}
}
}
operator features. I have one problem.
When I use this class in my program, say
CProductInfo product = null;
and later, when i check for emptiness,
if (product==null)
it returns false as if the product instance is not empty.
I tried various ways to eliminate this run-time error, but it just does
not happen. Also I don't think there is use full tips on net for this.
Can someone help?
Thanks in advance - Rhonald
<<<< THE SOURCE CODE FOR CProductInfo IS BELOW >>>>>
using System;
using System.Data;
using System.Data.SqlClient;
using MyDataLibrary;
namespace ProductManager2006
{
/// <summary>
/// Summary description for CProductInfo.
/// </summary>
public class CProductInfo:ConnectionManager
{
internal long m_AutoIndex = 0;
internal string m_SerialNumber = "";
public string Title = "";
private DateTime m_DateReference = DateTime.Now;
private DateTime m_DateStart = DateTime.Now, m_DateFinish =
DateTime.Now;
public ProductType Type = ProductType.CustomizedProduct;
public string Version = "", Build = "", Explanation = "";
public string Notes = "", Features = "", SystemRequirements = "",
PreRequisites = "";
public bool Status = true;
private bool m_IsDateStartAvailable = false, m_IsDateFinishAvailable
= false;
public CProductInfo()
{
//
// TODO: Add constructor logic here
//
}
public CProductInfo(string productId)
{
Open(productId);
}
public CProductInfo(DataRow dr)
{
if (dr!=null)
{
m_AutoIndex = DataConverter.ToLong(dr["auto_index"].ToString());
m_SerialNumber = dr["pk_product_id"].ToString();
m_DateReference =
Convert.ToDateTime(dr["date_reference"].ToString());
Title = dr["title"].ToString();
if (!dr.IsNull(4)) DateStart =
Convert.ToDateTime(dr["date_start"].ToString());
if (!dr.IsNull(5)) DateFinish =
Convert.ToDateTime(dr["date_finish"].ToString());
Type =
DataConverter.ToBoolean(dr["product_type"].ToString())?ProductType.GeneralizedProductroductType.CustomizedProduct;
Version = dr["version"].ToString();
Build = dr["build"].ToString();
Notes = dr["notes"].ToString();
Features = dr["features"].ToString();
SystemRequirements = dr["system_requirements"].ToString();
PreRequisites = dr["prerequisites"].ToString();
Status = DataConverter.ToBoolean(dr["status"].ToString());
}
}
private string _NewID
{
get
{
SqlCommand cmdSql = new SqlCommand("select isnull(max(auto_index),
0) as counter from mstrproducts", Connection);
try
{
OpenConnection();
SqlDataReader dr = cmdSql.ExecuteReader();
if (dr.Read())
{
return
((DataConverter.ToInteger(dr["counter"].ToString())+1).ToString("00000"));
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
finally
{
CloseConnection();
}
return null;
}
}
public string SerialNumber
{
get
{
return m_SerialNumber;
}
}
public DateTime DateReference
{
get
{
return m_DateReference;
}
}
public DateTime DateStart
{
get
{
return m_DateStart;
}
set
{
m_DateStart = value;
m_IsDateStartAvailable = true;
}
}
public DateTime DateFinish
{
get
{
return m_DateFinish;
}
set
{
m_DateFinish = value;
m_IsDateFinishAvailable = true;
}
}
public bool IsDateStartAvailable
{
get
{
return m_IsDateStartAvailable;
}
}
public bool IsDateFinishAvailable
{
get
{
return m_IsDateFinishAvailable;
}
}
public bool IsCustomBuilt
{
get
{
return Type == ProductType.CustomizedProduct?true:false;
}
}
public bool IsGeneralBuilt
{
get
{
return Type == ProductType.GeneralizedProduct?true:false;
}
}
public bool Open(string id)
{
SqlCommand cmdSql = new SqlCommand("select * from mstrproducts where
pk_product_id='"+id+"'", Connection);
try
{
OpenConnection();
SqlDataReader dr = cmdSql.ExecuteReader();
if (dr.Read())
{
m_AutoIndex = DataConverter.ToLong(dr["auto_index"].ToString());
m_SerialNumber = dr["pk_product_id"].ToString();
m_DateReference =
Convert.ToDateTime(dr["date_reference"].ToString());
Title = dr["title"].ToString();
if (!dr.IsDBNull(4))
{
DateStart = Convert.ToDateTime(dr["date_start"].ToString());
m_IsDateStartAvailable = true;
}
else
m_IsDateStartAvailable = false;
if (!dr.IsDBNull(5))
{
DateFinish = Convert.ToDateTime(dr["date_finish"].ToString());
m_IsDateFinishAvailable = true;
}
else
m_IsDateFinishAvailable = false;
Type =
DataConverter.ToBoolean(dr["product_type"].ToString())?ProductType.GeneralizedProductroductType.CustomizedProduct;
Version = dr["version"].ToString();
Build = dr["build"].ToString();
Notes = dr["notes"].ToString();
Features = dr["features"].ToString();
SystemRequirements = dr["system_requirements"].ToString();
PreRequisites = dr["prerequisites"].ToString();
Status = DataConverter.ToBoolean(dr["status"].ToString());
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
finally
{
CloseConnection();
}
return true;
}
internal bool Save()
{
SqlCommand cmdSql = new SqlCommand("RegisterProducts", Connection);
cmdSql.CommandType = CommandType.StoredProcedure;
if (m_SerialNumber.Trim().Length<=0) m_SerialNumber = _NewID;
cmdSql.Parameters.Add(new SqlParameter("@product_id",
SqlDbType.VarChar, 10)).Value = SerialNumber;
cmdSql.Parameters.Add(new SqlParameter("@date_reference",
SqlDbType.SmallDateTime)).Value =
Convert.ToDateTime(DateReference.ToShortDateString());
cmdSql.Parameters.Add(new SqlParameter("@title", SqlDbType.VarChar,
150)).Value = Title;
if (IsDateStartAvailable) cmdSql.Parameters.Add(new
SqlParameter("@date_start", SqlDbType.SmallDateTime)).Value =
DateStart;
if (IsDateFinishAvailable) cmdSql.Parameters.Add(new
SqlParameter("@date_finish", SqlDbType.SmallDateTime)).Value =
DateFinish;
cmdSql.Parameters.Add(new SqlParameter("@product_type",
SqlDbType.Bit)).Value = (Type == ProductType.GeneralizedProduct?0:1);
cmdSql.Parameters.Add(new SqlParameter("@version",
SqlDbType.VarChar, 15)).Value = Version;
cmdSql.Parameters.Add(new SqlParameter("@build", SqlDbType.VarChar,
15)).Value = Build;
cmdSql.Parameters.Add(new SqlParameter("@notes", SqlDbType.NVarChar,
500)).Value = Notes;
cmdSql.Parameters.Add(new SqlParameter("@features",
SqlDbType.NVarChar, 500)).Value = Features;
cmdSql.Parameters.Add(new SqlParameter("@system_requirements",
SqlDbType.NVarChar, 500)).Value = SystemRequirements;
cmdSql.Parameters.Add(new SqlParameter("@prerequisites",
SqlDbType.NVarChar, 500)).Value = PreRequisites;
cmdSql.Parameters.Add(new SqlParameter("@status",
SqlDbType.Bit)).Value = (Status?1:0);
try
{
OpenConnection();
cmdSql.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
return true;
}
internal bool Delete()
{
return false;
}
public static bool operator == (CProductInfo product1, CProductInfo
product2)
{
return product1.Equals(product2);
}
public static bool operator != (CProductInfo product1, CProductInfo
product2)
{
return (!product1.Equals(product2));
}
public override bool Equals(object obj)
{
if (!(obj is CProductInfo)) return false;
return this==(CProductInfo)obj;
}
public override int GetHashCode()
{
return base.GetHashCode ();
}
public override string ToString()
{
return base.ToString ();
}
}
}