Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

10 Experts available now in Live!

Get 1:1 Help Now