C
C#2y ago
Yashogi

# Trying to add GrossWeight each time it shows up in the list

Trying to learn how to use a ForEach loop while doing my homework. I feel like this should be easier than it is, but basically I am trying to add the grossweight by each other every time one is found in my list. Once I do that I need to add the weight of the engine and return the whole equation sum out of the method. If more code is needed please go ahead and ask, I'll provide.
24 Replies
Servator2y ago
you need another variable for total weight
Angius2y ago
Create a new variable outside of the loop and keep adding to that
Servator2y ago
grossweight changes for every object in list/array
Yashogi2y ago
I have one already called MaxGrossWeight should I pass that into the method?
Angius2y ago
Why? You want to return a sum from this method
Yashogi2y ago
Can't I use that for total weight?
Angius2y ago
Idk Should it store total weight?
Servator2y ago
You can but it's unnecessary
Yashogi2y ago
Oh nah it doesn't just seen It's horsepower * 2000 so I need to make a new variable right above the loop Ok so I've done that You can paste code into this chat and make it look fancy right?
Servator2y ago
\$codegif
Yashogi2y ago
``     private int CalculateGrossWeightOfCars() {            int totalGrossWeight;                        foreach (int GrossWeight in RailCars) {                GrossWeight += GrossWeight;            }        } ``
``     private int CalculateGrossWeightOfCars() {            int totalGrossWeight;                        foreach (int GrossWeight in RailCars) {                GrossWeight += GrossWeight;            }        } ``
Angius2y ago
Yep
Yashogi2y ago
Didn't turn out that good but we learning
Angius2y ago
Well, you're not using this variable yet
Yashogi2y ago
Yeah
Angius2y ago
You'll need some starting value for it. `0` would seem reasonable Then, in the loop, keep adding to that variable
Yashogi2y ago
Ok so I need my total weight to basically take in my grossweight each time
Angius2y ago
Instead of to `GrossWeight`
Yashogi2y ago
Would it be totalGrossWeight += GrossWeight?
Servator2y ago
`` public class Engine    {        public readonly string Model;        public readonly string SerialNumber;        public readonly int Weight;        public readonly int HorsePower;        public Engine(string model, string serialNumber, int weight, int horsePower) {``
`` public class Engine    {        public readonly string Model;        public readonly string SerialNumber;        public readonly int Weight;        public readonly int HorsePower;        public Engine(string model, string serialNumber, int weight, int horsePower) {``
It's a field of `Engine` When you're trying to set the weight of an engine, which engine's weight do you want to set? You need an instance, a specific engine that can have weight A class is just a blueprint, so to speak. The actual object is the instance of a class Besides that, yes, those fields are readonly, so they can only be set from the ctor
`` public Train(Engine engine) {            Engine = engine;            RailCars = new List<RollingStock>();        }   ``
`` public Train(Engine engine) {            Engine = engine;            RailCars = new List<RollingStock>();        }   ``