C
C#14mo ago
Ewan

❔ ✅ "Cannot convert from int to string" why cant I take away these values??

the code underlined in red gives me this message, why doesnt the subtraction work?
No description
7 Replies
Thinker
Thinker14mo ago
Console.WriteLine expects a string, you're giving it an int
Ewan
EwanOP14mo ago
its in brackets? then why does the trainnumber on the right work?
Thinker
Thinker14mo ago
long explaination but essentially Console.WriteLine can take any amount of arguments, and it doesn't care about the type of any arguments besides the first If you want to make a string out of that then you'll need to use interpolated strings.
Console.WriteLine($"{trainnumber - notlate} out of {trainnumber} trains were late");
Console.WriteLine($"{trainnumber - notlate} out of {trainnumber} trains were late");
Ewan
EwanOP14mo ago
ah i see thanks!
mg
mg14mo ago
yeah, it's trying to invoke one of these overloads: https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-7.0#system-console-writeline(system-string-system-object-system-object-system-object) https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-7.0#system-console-writeline(system-string-system-object()) which is why it wants your first argument to be a string the subtraction itself is totally fine and does produce an int, but that's not what Console.WriteLine() wants
Ewan
EwanOP14mo ago
ok i gotcha, thank you! yay
Accord
Accord14mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server