T
T
TheTalion2016-10-20 20:44:41
C++ / C#
TheTalion, 2016-10-20 20:44:41

How to compare objects written to a sheet by a certain parameter?

I can not understand how you can compare the strings written to the sheet. Here, for example, we create a typed sheet, write a couple of lines there, but what next? It is necessary that when the X or Y of the object of group 1 coincides with other groups, some kind of event occurs (well, empty, for example), but I can’t figure out how to do this.

public static class Mainf
  {
    public static void main()
    {
      var Objects = new List<Obj>();
Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 1 });
Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 2 });
Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 2 });
Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 2 });
Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 3 });
Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 3 });
    }
  }


    struct Obj
    {
      public int x, y, group;
      public Obj(int x, int y, int group)
      {
        this.x = x;
        this.y = y;
        this.group = group;
    }
    }

I will be glad to help. (preferably with code)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanostee, 2016-10-20
@TheTalion

1) Implement your list class by inheriting it from List;
2) Add an event called when a similar Obj object is added;
3) Implement the Add method (hiding a similar method of the base class), in case of adding a similar object, call the DuplObjAdded event.

public class DuplObjEventArgs : EventArgs
{
  public DuplObjEventArgs(Obj duplObj)
  {
    DuplObj = duplObj;
  }

  public Obj DuplObj { get; private set; }
}

public class ObjList : List<Obj>
{
  public EventHandler<DuplObjEventArgs> DuplObjAdded;

  public ObjList() : base()
  {
  }

  public new void Add(Obj newObj)
  {
    base.Add(newObj);

    if (this.Any(s => s.group != newObj.group && s.x == newObj.x && s.y == newObj.y))
    {
      OnDuplObjAdded(newObj);
    }
  }

  protected virtual void OnDuplObjAdded(Obj duplObj)
  {
    DuplObjAdded?.Invoke(this, new DuplObjEventArgs(duplObj));
  }
}

static void Main(string[] args)
{
  var Objects = new ObjList();

  Objects.DuplObjAdded += (o,e) => Console.WriteLine("DuplObj group: {0} x: {1} y: {2}", e.DuplObj.group, e.DuplObj.x, e.DuplObj.y);

  Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 1 });
  Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 2 });
  Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 2 });
  Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 2 });
  Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 3 });
  Objects.Add(new Obj { x = RandomRange(3000), y = RandomRange(3000), group = 3 });

  Console.ReadKey();
}

M
Maxim, 2016-10-22
@Got_Oxidus

Obj - convert to class.
add method

bool Compare(Obj fs, Obj sn)
{
   if(fs.x == sn.x && fs.y == sn.y && fs.group == sn group)
      return true;
   else return false;
}

PS Wrote the first thing that came to mind.
PPS Main capital letters

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question