Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
GeneralRe: Importing excel to datagrid .... Pin
nassimnastaran17-Oct-09 22:35
nassimnastaran17-Oct-09 22:35 
GeneralRe: Importing excel to datagrid .... [modified] Pin
dan!sh 17-Oct-09 23:06
professional dan!sh 17-Oct-09 23:06 
GeneralRe: Importing excel to datagrid .... [modified] Pin
nassimnastaran18-Oct-09 5:13
nassimnastaran18-Oct-09 5:13 
QuestionGenerics and the == operator. Pin
CaptainSeeSharp17-Oct-09 18:02
CaptainSeeSharp17-Oct-09 18:02 
AnswerRe: Generics and the == operator. Pin
N a v a n e e t h17-Oct-09 19:09
N a v a n e e t h17-Oct-09 19:09 
AnswerRe: Generics and the == operator. Pin
DaveyM6917-Oct-09 21:45
professionalDaveyM6917-Oct-09 21:45 
GeneralRe: Generics and the == operator. Pin
CaptainSeeSharp18-Oct-09 4:59
CaptainSeeSharp18-Oct-09 4:59 
AnswerRe: Generics and the == operator. Pin
dojohansen17-Oct-09 22:44
dojohansen17-Oct-09 22:44 
I don't understand where generics enter into the problem at all. You want to compare two TCell instances, and TCell is apparently not the generic type (not sure why else you would include the snippet to show that CellMap uses TCell).

Just overriding Equals or the equals operator should suffice.

class TCell
{
   int field1;
   string field2;

   // override equals...   
   override public bool Equals(object other)
   {
      if (!(other is TCell)) return false;
      return (field1 == other.field1 && field2 == other.field2);
   }

   // or overload the operator..
   static public bool operator ==(TCell x, TCell y) 
   {
      return (x.field1 == y.field1 && x.field2 == y.field2);
   }
}


The operator overloading syntax is unfamiliar, but it's just a method after all. Smile | :) The compiler will always look for user-defined operator implementations first and use the default implementations only if no user-defined operators exist. So here, since TCell is a class (a reference type), the compiler will use the user-defined == implementation only where an expression compares two objects of declared type TCell, and otherwise use a reference comparison.

(Of course, another possibility is to make TCell a value type, since the default == will then perform a value comparison rather than a reference comparison. But this alone would be no reason to choose to make TCell a value type!)
GeneralRe: Generics and the == operator. Pin
N a v a n e e t h17-Oct-09 23:18
N a v a n e e t h17-Oct-09 23:18 
GeneralRe: Generics and the == operator. Pin
dojohansen17-Oct-09 23:59
dojohansen17-Oct-09 23:59 
GeneralRe: Generics and the == operator. Pin
dojohansen18-Oct-09 0:00
dojohansen18-Oct-09 0:00 
QuestionDeleting objects from a collection Pin
Trollslayer17-Oct-09 10:34
mentorTrollslayer17-Oct-09 10:34 
AnswerRe: Deleting objects from a collection Pin
Not Active17-Oct-09 12:01
mentorNot Active17-Oct-09 12:01 
GeneralRe: Deleting objects from a collection Pin
Trollslayer17-Oct-09 12:19
mentorTrollslayer17-Oct-09 12:19 
AnswerRe: Deleting objects from a collection Pin
Luc Pattyn17-Oct-09 12:11
sitebuilderLuc Pattyn17-Oct-09 12:11 
GeneralRe: Deleting objects from a collection Pin
Trollslayer17-Oct-09 12:27
mentorTrollslayer17-Oct-09 12:27 
GeneralRe: Deleting objects from a collection Pin
CaptainSeeSharp17-Oct-09 13:16
CaptainSeeSharp17-Oct-09 13:16 
GeneralRe: Deleting objects from a collection Pin
Luc Pattyn17-Oct-09 13:26
sitebuilderLuc Pattyn17-Oct-09 13:26 
AnswerRe: Deleting objects from a collection Pin
DaveyM6917-Oct-09 12:51
professionalDaveyM6917-Oct-09 12:51 
GeneralRe: Deleting objects from a collection Pin
Trollslayer17-Oct-09 12:55
mentorTrollslayer17-Oct-09 12:55 
GeneralRe: Deleting objects from a collection Pin
DaveyM6917-Oct-09 13:10
professionalDaveyM6917-Oct-09 13:10 
GeneralRe: Deleting objects from a collection Pin
Trollslayer17-Oct-09 13:16
mentorTrollslayer17-Oct-09 13:16 
GeneralRe: Deleting objects from a collection Pin
Luc Pattyn17-Oct-09 13:33
sitebuilderLuc Pattyn17-Oct-09 13:33 
Answer[Added informatio] Deleting objects from a collection Pin
Trollslayer17-Oct-09 13:05
mentorTrollslayer17-Oct-09 13:05 
GeneralRe: [Added informatio] Deleting objects from a collection Pin
DaveyM6917-Oct-09 13:17
professionalDaveyM6917-Oct-09 13:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.