Well, simply, i learn about structs right now (new to C#) this is my struct (+ctor)
public struct BusLine
{
public int busNumber { get; set; }
public int passengersNumber { get; set; }
public double drivePrice { get; set; }
public double distanceBeginToEnd { get; set; }
public int stopsNumber { get; set; }
public BusLine(int _busNumber, int _passengersNumber, double _drivePrice, double _distanceBeginToEnd, int _stopsNumber)
{
busNumber = _busNumber;
passengersNumber = _passengersNumber;
drivePrice = _drivePrice;
distanceBeginToEnd = _distanceBeginToEnd;
stopsNumber = _stopsNumber;
}
}
and this is my Bus class
public class Bus
{
public BusLine Line { get; set; }
public int currentPassengers { get; set; }
public int currentStop { get; set; } = 1;
public void EnterStation(int newPassengers, int passengerLeaves)
{
currentPassengers = Line.passengersNumber + newPassengers - passengerLeaves;
}
public string Drive()
{
if (currentStop == this.Line.stopsNumber)
{
return string.Format("Stop number: {0}\nPassengers quantity: {1}\nThis is the final STOP!\n", currentStop, currentPassengers);
}
return string.Format("Stop number: {0}\nPassengers quantity: {1}\n", currentStop++, currentPassengers);
}
}
Now the problem is: When i instantiate both the struct and the class this way (within Program.cs):
Random rand = new Random();
BusLine BLine = new BusLine(_busNumber: 88, _passengersNumber: 22, _drivePrice: 6.90, _distanceBeginToEnd: 101.4, _stopsNumber: 15);
Bus bus = new Bus();
while (BLine.stopsNumber != 0)
{
BLine.stopsNumber--;
bus.EnterStation(rand.Next(1, 12), rand.Next(1, 12));
string getDrive = bus.Drive();
Console.WriteLine(getDrive);
}
it seems that is this line (within Bus.cs):
currentPassengers = Line.passengersNumber + newPassengers - passengerLeaves;
Line.passengersNumber = 0 always and
if (currentStop == this.Line.stopsNumber)
never occures since this.Line.stopsNumber is always zero now i understand that something with my use of structs is wrong, but i init them in the ctor of BusLine.cs, so how come their values is still 0 when using in Bus.cs? Thanks ahead guys
Linein yourbusobject. You would need to do this :bus.Line = BLine