C
C#2mo ago
Kimo_Terapy

rgument 2: cannot convert from 'int' to 'System.Func<int, decimal>'

C#
int lineReading;

if (inputStudentFile == "1")
{
inputStudentFile = "studenten-1.txt";
using (FileStream file = new FileStream(inputStudentFile, FileMode.Open, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(file))
{
while (!reader.EndOfStream)
{
lineReading = Convert.ToInt32(reader.ReadLine());
nums.Add(lineReading);
Console.WriteLine(lineReading);
}
if (inputMenu == "1")
{
nums.Sum(lineReading);
}
}
}
}
C#
int lineReading;

if (inputStudentFile == "1")
{
inputStudentFile = "studenten-1.txt";
using (FileStream file = new FileStream(inputStudentFile, FileMode.Open, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(file))
{
while (!reader.EndOfStream)
{
lineReading = Convert.ToInt32(reader.ReadLine());
nums.Add(lineReading);
Console.WriteLine(lineReading);
}
if (inputMenu == "1")
{
nums.Sum(lineReading);
}
}
}
}
Error (the title) is when I do nums.Sum(lineReading); idk why
31 Replies
Kimo_Terapy
Kimo_TerapyOP2mo ago
in short, the user has a "menu" where they can choose which .txt file to open. Then they get the option what to do with it. When they press "1" it will read the file, and add up all the numbers inside of that file and then do the sum with the numbers it has every number is seperated on each line in the files to make it easier to read. Thats why there is no Split()
mtreit
mtreit2mo ago
Why are you passing an argument to Sum() ?
viceroypenguin
viceroypenguin2mo ago
nums.Sum() takes a lambda, not a value you're tyring to do [1, 2, 3].Sum(4), which is non-sensical.
mtreit
mtreit2mo ago
You need to assign the value of nums.Sum() (no argument) to a variable.
viceroypenguin
viceroypenguin2mo ago
also, nums.Sum() does not change anything, it returns the sum calculated. so unless you print it (Console.WriteLine()) or store it (var x = ), it's useless
Kimo_Terapy
Kimo_TerapyOP2mo ago
oh yea pffft 🤦‍♂️ sorry i been making exercises for a few hours now
mtreit
mtreit2mo ago
result = nums.Sum()`
result = nums.Sum()`
Kimo_Terapy
Kimo_TerapyOP2mo ago
yea this is only a snippet of the code for the rest does it look ok
viceroypenguin
viceroypenguin2mo ago
it looks like it works. it's not how i would write it, but i'm not one to judge...
Kimo_Terapy
Kimo_TerapyOP2mo ago
still learning bro 😭
mtreit
mtreit2mo ago
inb4 someone says don't use Convert.ToInt32
viceroypenguin
viceroypenguin2mo ago
i'd do something like:
var result = File.ReadAllLines(inputStudentFile)
.Select(l => int.TryParse(l, out var val) ? val : 0)
.Sum();
var result = File.ReadAllLines(inputStudentFile)
.Select(l => int.TryParse(l, out var val) ? val : 0)
.Sum();
Kimo_Terapy
Kimo_TerapyOP2mo ago
looks clean but looks like chinese to me 😭
mtreit
mtreit2mo ago
BTW if you want to use StreamReader you can pass it the file name directly in the constructor. You don't need to first open a raw FileStream and then pass that in. So you can simplify that code a bit.
Kimo_Terapy
Kimo_TerapyOP2mo ago
wait whaaaaat
viceroypenguin
viceroypenguin2mo ago
also, yo udon't have to use {} with using
using var streamReader = new StreamReader(inputStudentFile);
while (!reader.EndOfStream)
{
}
using var streamReader = new StreamReader(inputStudentFile);
while (!reader.EndOfStream)
{
}
but File.ReadAllLines() gives you an array with every line of the file, so yo udon't even hve to mess with readers or while loops
Kimo_Terapy
Kimo_TerapyOP2mo ago
damn... then why they teaching it to us like this 😭
mtreit
mtreit2mo ago
Learning how to use streaming APIs is useful because sometimes you don't want to read the entire thing into memory. For instance if the file is gigabytes in size.
viceroypenguin
viceroypenguin2mo ago
var lines = File.ReadAllLines(inputStudentFile);
var numbers = new List<int>(lines.Length);
foreach (var line in lines)
numbers.Add(int.Parse(line));
var result = numbers.Sum();
var lines = File.ReadAllLines(inputStudentFile);
var numbers = new List<int>(lines.Length);
foreach (var line in lines)
numbers.Add(int.Parse(line));
var result = numbers.Sum();
Kimo_Terapy
Kimo_TerapyOP2mo ago
oh ok
viceroypenguin
viceroypenguin2mo ago
then this can be shortened to the version i gave you above by learning about LINQ and .Select()
mtreit
mtreit2mo ago
Another small thing you can fix if you want. The idiomatic way to stream through an entire text file is:
while (sr.ReadLine() is string line)
{
}
while (sr.ReadLine() is string line)
{
}
That way you get the line in one shot and don't need to use EndOfStream.
viceroypenguin
viceroypenguin2mo ago
yup. useful to knwo both streamreader and readalllines.
Kimo_Terapy
Kimo_TerapyOP2mo ago
so here sr.ReadLine(); assigns it to the variable line (if its a valid string of course)
viceroypenguin
viceroypenguin2mo ago
and if it's not null
mtreit
mtreit2mo ago
Yes, this is using a very neat feature of C# called pattern matching. When you pattern match on the result of a method call (using is in this case) you can assign it to a variable at the same time (in this case line) if it matches the pattern. It's very nice for this kind of thing.
Kimo_Terapy
Kimo_TerapyOP2mo ago
yea super chill
mtreit
mtreit2mo ago
If you want to learn more about pattern matching, @viceroypumpkin | 🦋🎃 did a very nice talk on it 🙂 https://youtu.be/w3m-THz--6A
.NET Foundation
YouTube
Solution2: Adventures in Pattern Matching with Stuart Turner
An in-depth guide to migrating existing code to use pattern matching! Join us on Discord: https://discord.com/invite/csharp JetBrains: https://www.jetbrains.com/ O'Reilly: https://www.oreilly.com
Kimo_Terapy
Kimo_TerapyOP2mo ago
appreciate it thats u in the vid? @viceroypumpkin | 🦋🎃
viceroypenguin
viceroypenguin2mo ago
aye, yes
Kimo_Terapy
Kimo_TerapyOP2mo ago
nice hat

Did you find this page helpful?