Is it possible to make array size size depended on the atribute value(int)? c# -
now question how can make when make new object type trip, arrayofpeople size of value numberofrooms?
class trip { private person[] arrayofpeople; public person[] arrayofpeople { return arrayofpeople; } set { arrayofpeople = value; } } class ship { private int numberofrooms; public int numberofrooms { { return numberofrooms; } set { numberofrooms = value; } } }
i thinking of making numberofrooms static , in trip constructor setting arrayofpeople = new person[ship.numberofrooms] not sure if right aproach. feel free correct me if wrong.
the comments in code answer question, check out :)
public class trip { // define constructor trip takes number of rooms // provided ship. public trip(int numberofrooms) { this.arrayofpeople = new person[numberofrooms]; } // removed field arrayofpeople becuase if // going set , return array without manipulating // don't need private field backing, property. private person[] arrayofpeople { get; set; } } public class ship { // define constructor takes number of rooms ship // ship knows it's room count. public ship(int numberofrooms) { this.numberofrooms = numberofrooms; } // removed numberofrooms field same reason // above arrayofpeople public int numberofrooms { get; set; } } public class myshipprogram { public static void main(string[] args) { // create ship x number of rooms var ship = new ship(100); // can create trip using ship's number of rooms var trip = new trip(ship.numberofrooms); } }
Comments
Post a Comment