C
C#

help

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.
SServator9/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
SServator9/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? You want to return a sum from this method
YYashogi9/27/2022
Can't I use that for total weight?
AAngius9/27/2022
Idk Should it store total weight?
SServator9/27/2022
You can but it's unnecessary
YYashogi9/27/2022
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?
SServator9/27/2022
$codegif
YYashogi9/27/2022
private int CalculateGrossWeightOfCars() {

int totalGrossWeight;

foreach (int GrossWeight in RailCars) {
GrossWeight += GrossWeight;
}
}
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 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?
SServator9/27/2022
also you need to return totalGrossWeight yeah
YYashogi9/27/2022
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) {
AAngius9/27/2022
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
YYashogi9/27/2022
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
How to read DateTime from SQLite database using System.Data.SQLite?I have a sqlite database with a DateTime entry. Since SQLite doesn't have a DateTime data type, it sBlazor VSCode --unable to find an installation of the browser on your system.-- [Answered]I'm getting an error `Unable to launch browser: "Unable to find an installation of the browser on yoCollection of different classesI have a situation where I need to store two different types together, and I'm not sure what's a gooNuGet behind a proxyHey I am in the process of releasing a .NET project as a Docker container. How can I configure NugetFluentAPI, Joining Table of the same entitiesI have entities User: id, payments; Payments: id, user1, user2 I want user to have n payments, and EF sets a property out of nowhere [Solved] [Answered]```cs public int RepliesLength { get; set; } = 0; public int RepliesCount { get; set; } = 0; publicJenkins unable to build CSharp project [Answered]any ideas why that might be?✅ Embedding an application WinUI 3This may be a stupid question, but is it possible to have the UI of a different program be a compone.Net 6 on IIS (Port conflict)Hey! I have an application that I would like to run on port 8081, and it does use port 8081 in startupload file that have huge size using asp net framework formhi all, i understand that we can't upload file to a form with size larger than 2GB using asp net (reCast failing on runtimeI have a construct. that i simplified in a little example seen below. I was wondering how i can resoNeed help on how to make an AOL style movable window inside of the main windowI wanna try to make a little AOL like program that has moveable windows inside of the main window wiNested KeyValuePair```private KeyValuePair<string, double> priceAndCurrency; private KeyValuePair<string, KeyVWhere is AssemblyInfo.cs [Answered]I just learned that I am not supposed to make my projects using the .NET Framework project templatesTrack gitignored ChangesI work on a software where the base product is an off the shelf vendor product and we have added cus.NET 6.0 not showing up in Visual Studio [Answered]I have .NET 6.0 installed, but it's not in the dropdown. How can I switch to .NET 6?Communication between variables in Multiple ThreadsHello, I am kinda new to this and I've never really worked with multi threads, decided to finally lCreating a interface for a template engine for multiple different typesI created a very basic template engine to scan a string for a token and replace it. I am struggling Native method that returns multiple random valuesEssentially, I am looking for a cleaner version of the following: ```cs var foo = new float[] { rndtough homework 4The school management held a poll on the readiness of the students to go on a trip. Each of the 200