?
Solved

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

Posted on 2011-03-17
5
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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