PC Review


Reply
Thread Tools Rate Thread

Compare structures with C#

 
 
Gianco
Guest
Posts: n/a
 
      15th Sep 2004
Hello people

I have the following problem, probably it has an easy solution but I
don't know how to solve it:

There's a structure with many fields, with this structure I create a
variable named, for instance 'A'. Then I create a second variable
named 'B'. I would save the complete content of A into B with one or
few commands (not copying field by field) and, later, check if A is
different from B, with C/C++ is very easy, first I make a memcpy of A
into B then, to see if something is different I can use the memcmp

How can do it with C#?

If I write 'B = A' B is just referenced to A so when I change
something into A, B has always the same content of A. And when I
compare A to B with 'if (A == B)' it gives me always true...

Sorry for the banal question, thanks in advance to anyone can help me

Gianco
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Sep 2004
Gianco <(E-Mail Removed)> wrote:
> I have the following problem, probably it has an easy solution but I
> don't know how to solve it:
>
> There's a structure with many fields, with this structure I create a
> variable named, for instance 'A'. Then I create a second variable
> named 'B'. I would save the complete content of A into B with one or
> few commands (not copying field by field) and, later, check if A is
> different from B, with C/C++ is very easy, first I make a memcpy of A
> into B then, to see if something is different I can use the memcmp
>
> How can do it with C#?
>
> If I write 'B = A' B is just referenced to A so when I change
> something into A, B has always the same content of A. And when I
> compare A to B with 'if (A == B)' it gives me always true...
>
> Sorry for the banal question, thanks in advance to anyone can help me


It sounds like you're actually dealing with classes, not structs.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Duncan Mole
Guest
Posts: n/a
 
      15th Sep 2004
As you say doing something like 'B = A' means that A and B are pointing to
the same object. Thereafter A == B will always return true. It sounds like
you want to implement a "deep clone". Theres a debate a over whether you
should use the ICloneable interaface, I think the concensus is to avoid it
as it you cannot make a distinction between a shallow or deep copy.

Once you have a deep copy you can implement IComparable such that the
following will test for equality:

YourType A;
YourType B;

if(A.CompareTo(B) == 0)
{
//Same value;
}

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Gianco <(E-Mail Removed)> wrote:
> > I have the following problem, probably it has an easy solution but I
> > don't know how to solve it:
> >
> > There's a structure with many fields, with this structure I create a
> > variable named, for instance 'A'. Then I create a second variable
> > named 'B'. I would save the complete content of A into B with one or
> > few commands (not copying field by field) and, later, check if A is
> > different from B, with C/C++ is very easy, first I make a memcpy of A
> > into B then, to see if something is different I can use the memcmp
> >
> > How can do it with C#?
> >
> > If I write 'B = A' B is just referenced to A so when I change
> > something into A, B has always the same content of A. And when I
> > compare A to B with 'if (A == B)' it gives me always true...
> >
> > Sorry for the banal question, thanks in advance to anyone can help me

>
> It sounds like you're actually dealing with classes, not structs.
>
> Could you post a short but complete program which demonstrates the
> problem?
>
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Gianco
Guest
Posts: n/a
 
      16th Sep 2004
Yes I supposed to work with a structure but it was declared as class,
now I changed something to use it as a structure and now make test if
the problem is solved. Thank you very much

Gianco
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting tabular structures in a Word document into an actual tableor reading data from the tabular structures using VBA code s Microsoft Word Document Management 9 3rd Jun 2010 11:30 PM
Structures inside structures Andrew Microsoft VB .NET 8 17th May 2009 11:45 PM
Structures inside structures Andrew Microsoft VB .NET 0 15th May 2009 07:23 AM
How do I compare cells and if FALSE compare to next cell in EXCEL =?Utf-8?B?Q2luZGll?= Microsoft Excel Worksheet Functions 0 24th Mar 2006 05:29 PM
Compare two directory structures and the files they contain Tyaan Microsoft Windows 2000 1 9th Dec 2003 07:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:39 PM.