Re: Compare Two Identical Datatable By Content

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi
I think its the same thing again . Even if you override the operators the
logic of comparisn remains the same i.e we still need to traverse each
aggregated object of the datable one by one

"Ciaran O''''Donnell" wrote:

sorry, forgot you have to do the != if doing ==

class MyDataTable : DataTable
{

public static bool operator==(MyDataTable dt1, MyDataTable dt2){
return dt1.Equals(dt2);
}

public static bool operator !=(MyDataTable dt1, MyDataTable dt2)
{
return !(dt1 == dt2);
}

public override bool Equals(object obj)
{
if (obj is DataTable && obj != null)
{
//Do Logic Here
return base.Equals(obj);
}
else
{
return false;
}

}
}

--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com


"Ciaran O''''Donnell" wrote:

You need to inherit from DataTable;

class MyDataTable : DataTable
{

public static bool operator==(MyDataTable dt1, MyDataTable dt2){
return dt1.Equals(dt2);
}

public override bool Equals(object obj)
{
if (obj is DataTable && obj != null)
{
//Do Logic Here
return base.Equals(obj);
}
else
{
return false;
}

}
}

--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com


"inpuarg" wrote:

On Mon, 18 Dec 2006 01:44:00 -0800, Ciaran O''Donnell
<CiaranODonnell@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

To be sure you would need to go through each cell in the entire table till
you find the first difference.
This is not effective for me cause there are too many rows in the
datatables.

You could put shortcuts in first like,
object.Equals to check if they are actually the same table, then compare row
counts and column counts, compare the schema (column datatypes). You could
try random sampling but it doesnt give you certainty.
I can see in debug window that data are same. But c# says theese are
not equal. Hashcodes are not same also. I think this is about .net 's
object compare model. Cause here is a sample :


Suppose that i have a class called Person :

public class Person
{
public string Name = "";
}

and let me test == operation :


Person a = new Person();
a.Name = "Person1";

Person b = new Person();
v.Name = "Person1";

if (a == b)
{
MessageBox.Show ("Equal");
}
else
{
MessageBox.Show("a : " + a.GetHashCode () + Environment.NewLine +
"b: " + b.GetHashCode() );
}


If you run this code you 'Ll see that theese objects are not equal.
Cause .net is not comparing theese objects due to their name 's
values (which is expected result - i am not against)
But there must be a way of overriding == operation for Person class
and make this comparison over Name 's values. And i know that this
exist too.

So here i am asking that is this possible for Datatable object ?
Is there such a method ? way , workarround etc.


.



Relevant Pages

  • Re: hashkey/digest for a complex object
    ... equality of two of these objects would first compare their hashkeys. ... whose keys are integers and values are strings. ... dictionary of dictionaries of dictionaries... ... def apply_method(f, obj): ...
    (comp.lang.python)
  • Collection Look Up - By Key in an Excel VBA App.
    ... Dim c As Collection ... The Lookup I havent been able to compare, ... since I havent been able to Look up an Obj by key from a collection. ...
    (microsoft.public.excel.programming)
  • RE: Equality vs Sameness
    ... > Good call on my misuse of overriding vs overloading. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compare two instance of a class
    ... If you override Equal you should also override GetHashCode. ... In most cases I think it is better to implement a specific method to compare ... reference type instances - value comparison is often a business/application ... override default language functionality. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why use Objectg in Equals method?
    ... superclasses (but not subclasses). ... Of course, if you intend to compare with objects of different classes, ... It is convenient that 'equals(Object o)' is an override - it allows for expressive compactness in a ton of algorithms, e.g., those on which collections classes rely. ...
    (comp.lang.java.programmer)