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

YYashogi9/27/2022
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.
Image
SSamarichitane9/27/2022
you need another variable for total weight
AAngius9/27/2022
Create a new variable outside of the loop and keep adding to that
SSamarichitane9/27/2022
grossweight changes for every object in list/array
YYashogi9/27/2022
I have one already called MaxGrossWeight should I pass that into the method?
AAngius9/27/2022
Why?
AAngius9/27/2022
You want to return a sum from this method
YYashogi9/27/2022
Can't I use that for total weight?
AAngius9/27/2022
Idk
AAngius9/27/2022
Should it store total weight?
SSamarichitane9/27/2022
You can but it's unnecessary
YYashogi9/27/2022
Oh nah it doesn't just seen
YYashogi9/27/2022
It's horsepower * 2000
YYashogi9/27/2022
so I need to make a new variable right above the loop
YYashogi9/27/2022
Ok so I've done that
YYashogi9/27/2022
You can paste code into this chat and make it look fancy right?
SSamarichitane9/27/2022
$codegif
YYashogi9/27/2022
     private int CalculateGrossWeightOfCars() {

            int totalGrossWeight;
            
            foreach (int GrossWeight in RailCars) {
                GrossWeight += GrossWeight;
            }
        } 
AAngius9/27/2022
Yep
YYashogi9/27/2022
Didn't turn out that good but we learning
AAngius9/27/2022
Well, you're not using this variable yet
YYashogi9/27/2022
Yeah
AAngius9/27/2022
You'll need some starting value for it. 0 would seem reasonable
AAngius9/27/2022
Then, in the loop, keep adding to that variable
YYashogi9/27/2022
Ok so I need my total weight to basically take in my grossweight each time
AAngius9/27/2022
Instead of to GrossWeight
YYashogi9/27/2022
Would it be totalGrossWeight += GrossWeight?
SSamarichitane9/27/2022
also you need to return totalGrossWeight
SSamarichitane9/27/2022
yeah
YYashogi9/27/2022
Ahh ok
YYashogi9/27/2022
And now for the last part my engine is a class that has a Weight given in the constructor
YYashogi9/27/2022
I thought passing in Engine.Weight would fit in my method but it says it cannot find it in context, my guess is that it's set to readonly?
YYashogi9/27/2022
 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) {
AAngius9/27/2022
It's a field of Engine
AAngius9/27/2022
When you're trying to set the weight of an engine, which engine's weight do you want to set?
AAngius9/27/2022
You need an instance, a specific engine that can have weight
AAngius9/27/2022
A class is just a blueprint, so to speak. The actual object is the instance of a class
AAngius9/27/2022
Besides that, yes, those fields are readonly, so they can only be set from the ctor
YYashogi9/27/2022
Ahh ok
YYashogi9/27/2022
My engine only has one Weight
YYashogi9/27/2022
Compared to my train which has multiple
YYashogi9/27/2022
 public Train(Engine engine) {
            Engine = engine;
            RailCars = new List<RollingStock>();
        }   
YYashogi9/27/2022
I have this passed into my train program
YYashogi9/27/2022
I just don't know what would be necessary to grab the weight from the engine class
YYashogi9/27/2022
Sorry if I sound like a noob I started c# 6 months ago
YYashogi9/27/2022
I think I'm close to figuring out the rest from here, thanks for the help guys It made much more sense when I realized I had to initialize the variable first.