0

What I want to do is compare two of the same variable in a structure. For example I have a structure like so:

    struct player
    {
        public string name;
        public int number;

    }
    static player[] players = new player[3];

and what I want to do is compare the numbers, so that if two players have the same number, something will happen.

This is what I tried, however it would always say two numbers were the same because it would compare two of the same

  for (int i = 0; i < length; i++)
        {
           for (int j = 0; j < length; j++)
            {
                if (players[i].number == players[j].number)
                {
                    Console.WriteLine("Same");
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("Not");
                    Console.ReadLine();
                }
            }

Hopefully you understand what I mean. Any help would be really appreciated! Thanks

lucycopp
  • 213
  • 1
  • 4
  • 18
  • It's not really clear what the problem is. Can you clarify? What is wrong with your code? – rory.ap Nov 30 '15 at 18:37
  • 6
    Also: you shouldn't be using `struct` here, stick to `class`. – Blorgbeard Nov 30 '15 at 18:40
  • @Blorgbeard Why not? – Ivan Stoev Nov 30 '15 at 18:48
  • @Blorgbeard -- Yeah, please elaborate, if not for us then for future people who view this post. – rory.ap Nov 30 '15 at 18:50
  • 3
    @IvanStoev structs are value-types, which means they don't act as you might expect in certain circumstances, especially if they are mutable, as OP's is. See: http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil – Blorgbeard Nov 30 '15 at 18:51
  • @Blorgbeard It's just an opinion. Mine (and not only) is contrary. We are using mutable structs for years and have absolutely no problem. Looking forward for C# ref returns and ref locals. Anyway, don't be purist, structs are very useful in many cases, and are one of the things that make difference in .NET vs Java. – Ivan Stoev Nov 30 '15 at 18:58
  • 3
    Sure, if you understand the implications of using a (mutable) struct, and have an actual problem they solve, go for it. For someone who's just learning C#, it's better to stick to classes. Personally, I basically follow [this](https://msdn.microsoft.com/en-us/library/ms229017.aspx). – Blorgbeard Nov 30 '15 at 19:04
  • 1
    Given the context of this question, I'd guess it's a game, and probably in unity or mono. Because they're handled differently in regard to garbage collection (stack vs heap storage), structs are used much more frequently in those development environments/frameworks. – MutantNinjaCodeMonkey Nov 30 '15 at 19:26

3 Answers3

3

Problem is in your loop variables i and j starting both at index zero. Then you are comparing element zero to element zero and therefore the condition is true.

Update this line:

 for (int j = 0; j < length; j++)

to this:

 for (int j = i + 1; j < length; j++)

Edit

To be more precise. The condition evaluates to true not only for the first element, but for each element when i and j are the same. This solution bars both control variables from having the same value in any iteration.

Community
  • 1
  • 1
Leandro
  • 1,560
  • 17
  • 35
  • Only for the first loop iteration of each j-loop...shouldn't be for all iterations... – rory.ap Nov 30 '15 at 18:39
  • For the first iteration of the outer loop, and the first iteration of the inner loop, it will compare elements 0 to 0, sure. But then the next iteration of the inner loop -- still the first iteration of the outer loop -- will compare element 0 to 1, and so on. I'm not disputing that *sometimes* you'll be comparing the same element. I'm just saying it's not true for *all* iterations, which is what it seems the OP was saying was happening. – rory.ap Nov 30 '15 at 18:46
  • I guess the point is: we're not clear what the OP means, hence my comment under the post to clarify. You can't answer a question that isn't clear. – rory.ap Nov 30 '15 at 18:50
  • The confusing part in the question is "it would **always** say two numbers were the same". That's what I'm disputing. – rory.ap Nov 30 '15 at 18:54
  • I think the `OP` meant that it happens once for each element in the array, because it would compare element zero to element zero, one with one and so on. – Leandro Nov 30 '15 at 18:55
1

Simple, just add a check to make sure you aren't comparing the same index, because this is the same object:

for (int i = 0; i < length; i++)
{
    for (int j = 0; j < length; j++)
    {
        if (i == j) continue;

        if (players[i].number == players[j].number)
        {
            Console.WriteLine("Same");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("Not");
            Console.ReadLine();
        }
    }
Marco Fatica
  • 937
  • 7
  • 12
-2

Use a Class, and do it using Linq:

public class Player
{
public string Name { get; set; }
public int Number { get; set; }
}

Then in the other class have this method to cross-check

    private void Match()
{
    var players = new Player[3].ToList();

    foreach (var found in players.ToList().Select(player => players.FirstOrDefault(p => p.Number == player.Number)))
    {
        if (found != null)
        {
            Console.WriteLine("Same");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("Not");
            Console.ReadLine();
        }
    }
}
Ammar Hamidou
  • 205
  • 2
  • 14