C
C#3y 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
Servator
Servator3y ago
you need another variable for total weight
Angius
Angius3y ago
Create a new variable outside of the loop and keep adding to that
Servator
Servator3y ago
grossweight changes for every object in list/array
Yashogi
YashogiOP3y ago
I have one already called MaxGrossWeight should I pass that into the method?
Angius
Angius3y ago
Why? You want to return a sum from this method
Yashogi
YashogiOP3y ago
Can't I use that for total weight?
Angius
Angius3y ago
Idk Should it store total weight?
Servator
Servator3y ago
You can but it's unnecessary
Yashogi
YashogiOP3y 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?
Servator
Servator3y ago
$codegif
Yashogi
YashogiOP3y 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;
}
}
Angius
Angius3y ago
Yep
Yashogi
YashogiOP3y ago
Didn't turn out that good but we learning
Angius
Angius3y ago
Well, you're not using this variable yet
Yashogi
YashogiOP3y ago
Yeah
Angius
Angius3y ago
You'll need some starting value for it. 0 would seem reasonable Then, in the loop, keep adding to that variable
Yashogi
YashogiOP3y ago
Ok so I need my total weight to basically take in my grossweight each time
Angius
Angius3y ago
Instead of to GrossWeight
Yashogi
YashogiOP3y ago
Would it be totalGrossWeight += GrossWeight?
Servator
Servator3y ago
also you need to return totalGrossWeight yeah
Yashogi
YashogiOP3y ago
Ahh ok And now for the last part my engine is a class that has a Weight given in the constructor 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?
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) {
Angius
Angius3y ago
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
Yashogi
YashogiOP3y ago
Ahh ok My engine only has one Weight Compared to my train which has multiple
public Train(Engine engine) {
Engine = engine;
RailCars = new List<RollingStock>();
}
public Train(Engine engine) {
Engine = engine;
RailCars = new List<RollingStock>();
}
I have this passed into my train program I just don't know what would be necessary to grab the weight from the engine class Sorry if I sound like a noob I started c# 6 months ago 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.
Want results from more Discord servers?
Add your server