473,406 Members | 2,352 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,406 software developers and data experts.

problem related to overloading == operator

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
3 1514
Constantine wrote:
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.


It's not clear what runtime error you're talking about. However,
general rules:

o Check whether you've been passed null (using object.ReferenceEquals)
in the Equals method.
o Use object.ReferenceEquals to check against a null reference for both
parameters in your == and != overloads, in order to avoid
NullReferenceExceptions.

Jon

Jun 29 '06 #2
Hi,

Next time just post the relevant code.
No idea what error are you talking, unless that you have something like:

CProductInfo product = null;
if (product==null) product = new CProductInfo ();

product.SomeAction();
Of course as you are seeing the if is always false and therefore the
instance may never be initialized.
**** update ****
ok, I think I know where the error is, it's in :

public static bool operator == (CProductInfo product1, CProductInfo
product2)
{
return product1.Equals(product2);
}

you are presuming that the first argument of "==" is an initialized instance
when it's not there you will get a NullReference Exception.
**** end of update ****

See Jon's comments to how to do it.

IMO it's not a very good idea overload == , the operator has a VERY CLEAR
meaning of comparing references, not members's values of those instances.

WARNING:

Also you may be getting in an infinite recursion, you call Equals from "=="
and inside Equals you again call "=="

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jun 29 '06 #3
Constantine wrote:
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

<snippedy-doo-dah>

Hi Rhonald,

You have a slight bug in your code, which I'm guessing hasn't surfaced
because you have a bit of exception handling somewhere.

You've got to make sure 'product1' in your operator overloads isn't null,
before you call the 'Equals' method.

--
Hope this helps,
Tom Spink
Jun 29 '06 #4

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...
2
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.