user5955442
user5955442

Reputation:

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;
    }

Upvotes: 0

Views: 112

Answers (2)

ADyson
ADyson

Reputation: 62078

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;
  }
}

Upvotes: 2

Visual Vincent
Visual Vincent

Reputation: 18320

There's some lack of code in your question, but to me it looks like this:

In every new Master class you initialize a new Arena instance:

Arena arena = new Arena();

then, in every new Arena class you initialize a new Master class:

Master master = new Master("");

So you seem to be initializing an infinite amount of Arena and Master classes. For every new Arena there'll be a new Master, and for every new Master there'll be a new Arena.


If you want to use the methods from the Arena and Master classes without having to create an instance all the time, you should declare them as public static. This makes them accessible without having to declare a class instance.

For example:

public class Arena
{
    public static void DoSomething()
    {
        //Do something...
    }
}

public class Master
{
    public static void AnotherStaticMethod()
    {
        //Do something here too...
    }
}

Then you'll be able to access the methods by calling either Arena.DoSomething() or Master.AnotherStaticMethod().

Upvotes: 0

Related Questions