Solved

See if two objects are referencing the same object in their list of variables

Posted on 2011-03-17
5
263 Views
Last Modified: 2012-05-11
Hi,

I'm not sure if I have a complicated problem or if I implemented my code wrong.

I have a Structure, lets call it struct, and in struct one of it's variables is a typeVar with autoimplemented properties

I then have a Structure, lets call it structStruct and it contains 2 autoimplemented property variables of type struct, lets say item1 and item2

I then have a List<struct> list and add to it two structStruct objects which reference the same typeVar:
struct item1 = typeVar;
struct item2 = typeVar;
list.Add(item1);
list.Add(item2);

now, in struct, I have an overloaded == operator implementation which says something like:
return (item1.item == item2.item);

But an if statement with list[0].item1 == list[1].item1 should return true, but it returns false.
An if with item1 == list[0].item1 returns true at least, which is correct.

I need to be able to get true from all the following statements:
list[0].item1 == list[1].item1
item1 == item2
item1 == list[0].item1
item1 == list[1].item1
item2 == list[0].item2
item2 == list[1].item2
etc

Any ideas?  Attached is some basic source code to help clarify a bit
public struct LinkItem
    {
        public FrameworkElement Item { get; set; }

        public LinkItem(FrameworkElement Item)
        : this()
        {
            this.Item = Item;
        }

        public static bool operator ==(LinkItem linkItem1, LinkItem linkItem2)
        {
            if (linkItem1 == null || linkItem2 == null)
                return false;

            return linkItem1.Item == linkItem2.Item
        }
    }

    public struct Link
    {
        public LinkItem item1 { get; set; }
        public LinkItem item2 { get; set; }

        public Link(FrameworkElement item1, FrameworkElement item2)
        : this()
        {
            this.item1 = new LinkItem(item1);
            this.item2 = new LinkItem(item2);
        }
    }

    public class Linker : IList<Link>
    {
        private List<Link> Links;

        public CreateLink(FrameworkElement item1, FrameworkElement item2)
        {
            Link link = new Link(item1, item2);
            Links.Add (link);
        }
    }

    public class Test
    {
        Canvas item = new Canvas();

        Linker linker = new Linker();
        
        linker.CreateLink (item, item);
        linker.CreateLink (item, item);
        
        output.write(item == item);
        output.write(linker.Links[0].item1 == item);
        output.write(linker.Links[0].item2 == item);
        output.write(linker.Links[0].item1 == linker.Links[0].item1);
        output.write(linker.Links[0].item1 == linker.Links[0].item2);
        output.write(linker.Links[0].item1 == linker.Links[1].item1);
        etc, etc...
    }

Open in new window

0
Comment
Question by:Cyber-Storm
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:mac-will
ID: 35158543
Remember structs in C# are value types not refrence types so when you pass them to the objects constructors a copy is made.
0
 
LVL 8

Accepted Solution

by:
mac-will earned 500 total points
ID: 35158670
This setup seems to be very convoluted.

Here is a test program (that actually compiles and runs) it prints all "true" for the outputs:
    class Program
    {
        static void Main(string[] args)
        {
          Canvas item = new Canvas();

        Linker linker = new Linker();
        
        linker.CreateLink (item, item);
        linker.CreateLink (item, item);
        
        Console.WriteLine(item == item);
        Console.WriteLine(linker.Links[0].item1.Item == item);
        Console.WriteLine(linker.Links[0].item2.Item == item);
        Console.WriteLine(linker.Links[0].item1 == linker.Links[0].item1);
        Console.WriteLine(linker.Links[0].item1 == linker.Links[0].item2);
        Console.WriteLine(linker.Links[0].item1 == linker.Links[1].item1);


            Console.Read();
        }
    }


    public struct LinkItem
    {
        public FrameworkElement Item { get; set; }

        public LinkItem(FrameworkElement Item)
        : this()
        {
            this.Item = Item;
        }

        public static bool operator ==(LinkItem linkItem1, LinkItem linkItem2)
        {
            if (linkItem1 == null || linkItem2 == null)
                return false;

            return linkItem1.Item == linkItem2.Item;
        }

        public static bool operator !=(LinkItem linkItem1, LinkItem linkItem2)
        {
            if (linkItem1 == null || linkItem2 == null)
                return true;

            return !(linkItem1.Item == linkItem2.Item);
        }
    }

    public struct Link
    {
        public LinkItem item1 { get; set; }
        public LinkItem item2 { get; set; }

        public Link(FrameworkElement item1, FrameworkElement item2)
        : this()
        {
            this.item1 = new LinkItem(item1);
            this.item2 = new LinkItem(item2);
        }
    }

    public class Linker
    {
        public List<Link> Links = new List<Link>();

        public void CreateLink(FrameworkElement item1, FrameworkElement item2)
        {
            Link link = new Link(item1, item2);
            Links.Add (link);
        }
    }

    public class Canvas : FrameworkElement
    {
    }

    public class FrameworkElement
    {
    }

Open in new window

0
 

Author Comment

by:Cyber-Storm
ID: 35159227
Yeah, sorry, I just coded quickly in the code window, didn't think you'd want to run it as much as just get a feel for it.  Pseudocode in some places to get the idea across than actual compiler code since I didn't think about trying it in a console app and just pastng that code.

It seems convoluted in the basic form but it's actually quite clean in it's true form, over a thousand lines though so it didn't feel practical attaching it.

Am I missing something or is our code mostly alike?  I must be tired but I'm not seeing an obvious difference.
0
 

Author Closing Comment

by:Cyber-Storm
ID: 35159528
ok, I feel like an idiot, my code was fine, I was just comparing the wrong items :(

Thanks very much for your help, at least knowing it was actually doable and see it working led me to think in a different way.

Thanks :)
0
 
LVL 8

Expert Comment

by:mac-will
ID: 35160459
Good luck.

mac
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access properties in nested observable collections 8 23
coded character set iptc field within a jpeg 1 69
C# HTTP GET method sample code 3 41
Wav problem 4 16
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now