Solved

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

Posted on 2011-03-17
5
268 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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…

688 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