c# - How to properly use instances in this example? -
i have multiple classes , want use functions in other classes. i'm facing problem , might know how 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 insert here? dont want iniciate again string ip = master.ip_dron(); ip_drone = ip;
the problem in master master = new master("");
if remove works cant use class. if use problem crash once forms master , arena open. how can instantiate instance correctly?
error:
make sure not infinite loop or infinite recursion.
edit: problem since class inicio open 2 different instances master, use 2 different ips. when run 2 instances, ip ip1.text , ip2.text. since open @ same time return ip_drone return last value ( in 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; }
if understand problem correctly, think need supply arena class specific instances of master want use. @ moment you're creating brand new instances of master , arena in each of classes respectively, causing infinite loop (master creates , arena, creates master, creates , arena, etc. etc. forever).
i think may solve problem:
class 1 inicio:
master master = new master(ip1.text); master slave = new master(ip2.text); arena arena = new arena(master, slave); //here pass specific instances of master arena class 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 not such name class...but that's story { //here assign instances passed in our internal variables, can reference them within arena class itself. this.master = master; this.slave = slave; } //and example (this may not want do): public string show() { string masterip = this.master.ip_dron(); string slaveip = this.slave.ip_dron(); return "master ip: " + masterip + ", slave ip: " + slaveip; } }
Comments
Post a Comment