Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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