473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

problem related to overloading == operator in c#

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.GeneralizedProduct:Produc tType.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.GeneralizedProduct:Produc tType.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 ();
}

}
}

Jun 29 '06 #1
2 1673

"Constantine" <rh**********@gmail.com> wrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
using System;
using System.Data;
using System.Data.SqlClient;
using MyDataLibrary;


Off-topic. Please see:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Regards,
Sumit.
Jun 29 '06 #2
On 29 Jun 2006 03:38:00 -0700, "Constantine" <rh**********@gmail.com>
wrote:

[snip]

Your question is about C#, but this is a C++ group. You will find
people who know a lot more about C# than we do if you ask your
question in a C# group like microsoft.public.dotnet.languages.csharp.

rossum

Jun 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
3
by: Constantine | last post by:
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...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.