public void AddGun(Gun gun) { bool add = true; foreach (Gun g in collectedGuns) { // Check if add is true? and whether the type of gun you are passing is in collected gun list //(g.GetType() == gun.GetType()); //if they are both true then it calls reloadAmmo method for g // Call the changegun method passing g to it and then set add to false if (add == true && (g.GetType() == gun.GetType())) { g.ReloadAmmo(); ChangeGun(g); add = false; } } if (add == true) { ChangeGun(gun); collectedGuns.Add(gun); } else { gun.Dispose(); } // once foreach loop finished, check the add variable, if true then Call ChangeGun method, pass gun to it // and add the gun to the collectedGun list, else call the Dispose method from gun }
public void AddGun(Gun gun) { //set a local bool variable named add to true bool add = true; // ChangeGun(gun); //and then for each gun g in the collectedGuns list check whether add is true and whether the type of gun you are passing //is in the collected gun list (g.GetType()==gun.GetType()), for (int g = 0; g < collectedGuns.Count; g++) // Loop through List with for { if (g.GetType() == gun.GetType() && add) { //if they are both true then it calls the reloadAmmo mehtod for g, //call the ChangeGun method passing g to it and then set add to false. collectedGuns[g].ReloadAmmo(); ChangeGun(collectedGuns[g]); add = false; } } //Once the for each loop finished check the add variable, if true then call CangeGun method and pass gun to //it else call the Dispose method from gun. if (add) { ChangeGun(gun); } else { gun.Dispose(); } }
//Finally implement a public void AddGun method which takes a Gun object named gun as parameter /// <summary> // Adds the gun to the armoury, and equips it /// </summary> public void AddGun(Gun gun) { bool add = true; //set a local bool variable named add to true foreach (Gun g in collectedGuns) { //for each gun g in the collectedGuns list check add is true and the type of gun passing is in the collected gun list (g.GetType()==gun.GetType()) if (add == true && g.GetType() == gun.GetType()) { g.ReloadAmmo(); //if they are both true then it calls the reloadAmmo method for g ChangeGun(g); //call the ChangeGun method passing g to it add = false; //set add to false. } } if (add == true) //Once the for each loop finished check the add variable { ChangeGun(gun); //if true then call ChangeGun method, pass gun to it collectedGuns.Add(gun); //and add the gun to the collectedGun list } else { gun.Dispose(); //else call the Dispose method from gun } }
/// <summary> /// This method adds a new gun to the armoury collection /// </summary> /// <param name="gun"></param> public void AddGun(Gun gun) { bool add = new bool(); add = true; foreach (Gun g in collectedGuns) { if (add && g.GetType() == gun.GetType()) { g.ReloadAmmo(); ChangeGun(g); add = false; } } if (add) { ChangeGun(gun); collectedGuns.Add(gun); } else { gun.Dispose(); } }