Why is my list output not using the method to check true/false
my output is printing like this:
Justyn Cushing, Week 2 Interface PA
Vehicles printed from List
Make: Ford
Model: Festiva
The car is running and heading to Miami, FL
Make: Honda
Model: Accord
The car is running and heading to Atlanta, GA
Make: Volvo
Model: VNL 860
The truck is running and hauling 150 cubic ft of cargo to Richmond, VA
Make: Ford
Model: Transit
The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA
Vehicles printed directly
Make: Ford
Model: Festiva
The car is running and heading to Miami, FL
Make: Honda
Model: Accord
The car is not running and heading to Atlanta, GA
Make: Volvo
Model: VNL 860
The truck is not running and hauling 150 cubic ft of cargo to Richmond, VA
Make: Ford
Model: Transit
The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA
The directly output is correct however, the list will not put anything other than all true or all false. I can upload other classes if needed.
25 Replies
my bool for true / false has a check to change it to is or is not so that it doesnt say true running or false running but that method only seems to be working in the direct
I don't see any if statements in what you sent
Is there more?
Oh wait I think I see the issue
trying to add the other classes
Presumably
Vehicle.Drive()
takes in a boolean representing whether or not it's running and the destination rightyes its
Well, the reason it's behaving that way in your foreach loop is that you're passing
true
every time
If the boolean that represents whether or not the vehicle is running comes from the vehicle class itself, it shouldn't be a parameter to the Drive()
methodso i tried not having true but then it tells me im missing a paramtere for that bool and fails to run.. i added true after trying chat gpt
Could you post the entire classes for car, truck, and IVehicle $paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
yeah one sec
Don't try chatgpt
If googling doesn't get you the answer just come here and ask actual people
BlazeBin - sfrjmejrqped
A tool for sharing your source code with the world!
yeah chat gpt had me redo the whole thing because it royally messed it up
yeah it'll do that to you
Ok there are a lot of things to talk about here but I'll start with what's most relevant to your issue
Like I mentioned, because
Car
and CargoTruck
both have fields m_running
, that shouldn't be a parameter to Drive()
In the Drive()
method, you can write Console.WriteLine("The car" + (m_running ? ...
Similarly for destination
It's what's referred to as encapsulation. The Car
class contains everything about the car, so those things shouldn't be passed in via parameters, they should be supplied by the class's members
Onto things that don't directly have anything to do with your issue, but are important if you're gonna write good C# code; instead of having fields m_make
and methods GetMake()
etc. you should have properties https://learn.microsoft.com/en-us/dotnet/csharp/propertiesso it should just be
public void Drive(string destination)
?What you're doing is something you'd see in C++, which doesn't have properties
It shouldn't have any parameters
yeah i know its sloppy and can be simplified but this is how the teacher wants it done lol
Both running and destination come from the class, not from outside
I was worried this was gonna be the answer
In that case keep it that way to appease your teacher but know that it is totally wrong to do things that way in C# lol
yeah i seem to hear that alot.. the joys of school.. so when i remove the parameters my writeline for running? and destination error because they dont exist in the context
Right, you need to replace them with
m_running
and m_destination
, respectively
Those two things being members of the class are how you represent the concept that the car knows whether its running and where it's going; those two things don't need to be supplied to it from the outsideoh wow it really was such a simple fix thank you.. idk why our guided assignment has us do it the way i had it and it works then when i reference that for this one it doesnt work that way
This does not strike me as a high quality class
But yeah, in C# the whole
m_variable
, GetVariable()
, SetVariable()
trio can be replaced by simply Variable { get; set; }
Fields do have their place and are prefixed with a single _
. They're used when the variable doesn't need to be exposed, so tl;dr when it's privatenone of them are.. be happy you havent seen some of my other classes .. and yeah get set is a lot simpler
Oh trust me I've been through my own CS hell
> puts an HTTP request on the screen
"so this is like, pseudocode"
lets put it this way.. i graduate in less than a year and i have no idea what im doing other than basic loops and making tiny programs that will never be used in the workforce 😄
thanks again! Ive been stuck on that for like 3 hours lol....