0

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

3
  • I would suggest that you start with setting breakpoints and using the debugger to step through the code.. Commented Mar 20, 2016 at 21:06
  • You're never instantiating the instance of Line in your bus object. You would need to do this : bus.Line = BLine Commented Mar 20, 2016 at 21:06
  • could you please reduce this to a minimal version of the problem? it seems like there's a ton of noise here that isn't really related. Commented Mar 20, 2016 at 21:14

1 Answer 1

6

It looks like you never set BLine to be the Line in bus. Change that bus declaration to:

Bus bus = new Bus() { Line = BLine };

Until you do that, BLine has nothing to do with bus.Line aside from just being of the same type.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked. I tought that if you init BusLine ctor within Program.cs it should be sufficient to use the variable Line of data type BusLine within Bus.cs
No, because in your question, you instantiated two separate BusLines: one that lives in Program and one that lives in bus. The one sitting in Program has all the info you entered, but the one inside bus still has its default values. If that solved your problem, I would appreciate you marking my answer as such. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.