Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Object-Equals method did not work

Started by chrisliando Jan 21, 2008 at 2:44 AM 0 replies 450+ views
Original Post
chrisliando
chrisliando
I have an Object named Support. Inside that Object, I override the original Object->Equal method to suit my purpose. The purpose is to check and avoiding data duplication when the user enter new data or edit the existing data. It really really did not work. I have debug it and when I came to that part, the variable bool sama here is still generate false even when the data is the same. Variable sama should be true when the data is equals. I also have tried to use the original Object->Equals method but the result is the same. What is wrong? How to solve this? Thank you very much. for (int i = 0 ; i Count ; i++) { Support ^currsupp = dynamic_cast(lstsupport); if (supp->Equals(currsupp)) sama = true; else //dataForm->dupObj = false; sama = false; } //for int i ref class Support : Object { public: String ^SupportDate; String ^SupportName; String ^SupportTime; String ^CustComp; String ^CustName; String ^SupportType; String ^FinishTime; public: Support(){ SupportDate = ""; SupportName = ""; SupportTime = ""; CustComp = ""; CustName = ""; SupportType = ""; FinishTime = ""; } Support(String ^sdate, String ^sname,String ^stime, String ^ccomp, String ^cname, String ^stype, String ^ftime){ SupportDate = sdate; SupportName = sname; SupportTime = stime; CustComp = ccomp; CustName = cname; SupportType = stype; FinishTime = ftime; } String^ writeLine(void) { return String::Concat(SupportDate,",",SupportName,",",SupportTime, ",",CustComp,",",CustName,",",SupportType,",",FinishTime); } virtual bool Equals(Object ^obj) override { return this == dynamic_cast(obj); } static bool operator==( Support x, Support y ) { return x.SupportDate == y.SupportDate && x.SupportName == y.SupportName && x.SupportTime == y.SupportTime && x.CustComp == y.CustComp && x.CustName == y.CustName && x.SupportType == y.SupportType && x.FinishTime == y.FinishTime; } ~Support() { } };
Machaira
Machaira
Your for loop is hosed. The first time you drop into the else you need to kick out of the loop and return false. There's no need to do any further checks.
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.