Search code examples
c#classinstances

How to properly use instances in this example?


I have multiple classes and I want to use the functions in other classes. But I'm facing a problem and you might know how to solve it.

Class 1 Inicio:

Master master = new Master(ip1.Text);
master.Show();

Master slave = new Master(ip2.Text);
slave.Show();

Arena arena = new Arena();
arena.Show();

Class 2 Master:

Arena arena = new Arena();

public Master(string ip) //Inicio
{
    InitializeComponent();

    _droneClient = new DroneClient("192.168.1." + ip);
    ip_drone = "192.168.1." + ip; 
    Point p2 = arena.posicao_desej();
    posicao_desejada = p2;

    public string ip_dron()
    {
         return ip_drone;
    }

Class 3 Arena:

Master master = new Master(""); //What do I insert here? I dont want to iniciate it again

string ip = master.ip_dron();
ip_drone = ip;

The problem is in Master master = new Master(""); If I remove it everything works but I cant use anything from that class. If I use like this the problem will crash once the forms Master and Arena are open. How can I instantiate the instance correctly?

ERROR:

Make sure you do not an infinite loop or infinite recursion.

EDIT: My problem is that since class Inicio will open two different instances from Master, it will use two different ips. When I run the two instances, ip will be ip1.text and then ip2.text. But since they open at the same time return ip_drone will only return the last value ( in this case ip2.text)

   public Master(string ip) //Inicio
    {
        InitializeComponent();

        _droneClient = new DroneClient("192.168.1." + ip);
        ip_drone = "192.168.1." + ip;  
    }

   public string ip_dron()
    {
        return ip_drone;
    }

Solution

  • If I now understand the problem correctly, I think you need to supply your Arena class with the specific instances of Master that you want to use. At the moment you're creating brand new instances of Master and Arena in each of those classes respectively, which is causing the infinite loop (Master creates and Arena, which creates a Master, which creates and Arena, etc. etc. forever).

    I think this may solve the problem:

    Class 1 Inicio:

    Master master = new Master(ip1.Text);
    Master slave = new Master(ip2.Text);
    Arena arena = new Arena(master, slave); //here we pass specific instances of Master to the Arena class for later use.
    arena.Show();
    

    Class 2 Master:

    class Master
    {
      public Master(string ip)
      {
        InitializeComponent();
        _droneClient = new DroneClient("192.168.1." + ip);
        ip_drone = "192.168.1." + ip; 
        Point p2 = arena.posicao_desej();
        posicao_desejada = p2;
    }
    
      public string ip_dron()
      {
         return ip_drone;
      }
    }
    

    Class 3 Arena:

    class Arena
    {
      private Master master;
      private Master slave;
    
      public Arena(Master master, Master slave) //hint: maybe Master is not such a good name for the class...but that's another story
      {
       //here we assign the instances passed in to our internal variables, so we can reference them within the Arena class itself.
        this.master = master;
        this.slave = slave;
      }
      //and then for example (this may not be what you really want to do):
      public string Show()
      {
        string masterIP = this.master.ip_dron();
        string slaveIP = this.slave.ip_dron();
        return "master IP: " + masterIP + ", Slave IP: " + slaveIP;
      }
    }