-2

I have this code

Class location
{
     int X{Get; Set;}
     int Y{Get;Set;}
}

List<location> mylst = new List<location>();
Public Void SetupList()
{
    for(int i =0; i<8; i++)
    {
       for(int b=0; b<8; b++)
       {
         location loc = new location();
         loc.x = b;
         loc.y = i;
          mylst.Add(loc);
       }
    }
}

Once the list is populated i want to be able to search through the list and see if any of the items in the list contain a location i.e

/// lets search for location 4,4
location tofind = new location()
tofind.x=4;
tofind.y=4;
foreach(location loc in mylst)
{
   if(loc == tofind)
   {
       ///delete that item from the list  

   }
}

but i am at a total loss on how to do this...

Any ideas would be helpful as nothing i have tried has worked

serdar
  • 1,564
  • 1
  • 20
  • 30
Mark W
  • 1
  • 2
  • Override the `==` operator or the `Equals()` function (and use it), right now you're checking for pointer equality. – Maximilian Gerhardt Feb 14 '16 at 21:43
  • and how do i override it? im new to lists – Mark W Feb 14 '16 at 21:44
  • 1
    You might want to put in some effort to make the code you post actually compile... – Patrick Hofman Feb 14 '16 at 21:46
  • the code is snippet from that it can be worked out i am trying to do – Mark W Feb 14 '16 at 21:47
  • @ Patrick and where exactly is this duplicate?? rather than just marking it why not inform me of where this is and i would gladly look at it and then see if it is what i am after – Mark W Feb 14 '16 at 21:50
  • I have taken the liberty to un-mark this as a duplicate. Overriding Equals is *one way* to solve the OPs problem, but not the only one. In general, I don't consider "How do I implement ?" to be a duplicate for "How do I solve problem X?". – Heinzi Feb 14 '16 at 21:50
  • @MarkW: The duplicate link can be found at the top of your question. Or rather... it *could* be found, because I just un-marked your question as a duplicate. In general, however, Patrick is right: *If* you question is a duplicate, closing it as a duplicate is the right thing to do. – Heinzi Feb 14 '16 at 21:52
  • exactly Heinzi and what you have actually posted is what i needed exactly – Mark W Feb 14 '16 at 21:52
  • 1
    Better duplicate : http://stackoverflow.com/q/10870131/993547 – Patrick Hofman Feb 14 '16 at 21:53
  • @PatrickHofman: Yep, good find! Feel free to close, I won't object. – Heinzi Feb 14 '16 at 21:54
  • You know I already used my vote. You on the other hand have one left. Feel free ;) @heinzi – Patrick Hofman Feb 14 '16 at 21:55
  • @Heinzi i noticed the duplicate notice above however that solution was far too complex for what i needed to do yours fits exact – Mark W Feb 14 '16 at 21:57
  • @PatrickHofman: Oh, didn't know that the vote stays "in use" after reopening. Closed. – Heinzi Feb 14 '16 at 21:57
  • Patrick i agree i didnt seem to find that one when searching – Mark W Feb 14 '16 at 21:58
  • Yeah, to prevent close wars I guess. – Patrick Hofman Feb 14 '16 at 21:58
  • @MarkW: Glad I could help! The new duplicate should be better, it has examples for both options 1 and 2 of my answer. – Heinzi Feb 14 '16 at 21:58

1 Answers1

3

You have two options:

  1. Override Equals/== and GetHashCode. That way, loc == tofind will yield true when the values of the two instances match.

  2. Search by explicitly comparing values:

    var listFound = myList.Where(loc => loc.X == tofind.X && loc.Y == tofind.Y).ToList();
    
Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519