Hello! Please tell me how in WinForme to make the program read data from a .txt file and convert it to numbers? And after that, these numbers are added and provided the result. In principle, with the addition, I probably will cope myself, but I will still be grateful for the hint.

  • one
    Add a question with your code (even if not working). - 0xdb
  • You have not shown anything; not your code, not even the contents of the file you want to read! - Bulson
  • Files can be absolutely any. There is only one condition that they contain strings of integers. - komsonavt
  • How many numbers in a row, what is the sign of separation between numbers? - Bulson
  • There is one number in one line, there are several lines in the file, but there may be a space after or before the numbers, and an empty line can also be caught! - komsonavt

1 answer 1

Very easy!

File.ReadLines(path) // прочитать строки .Select(s => s.Trim()) // обрезать пробелы в начале и в конце .Where(s => s != "") // выкинуть пустые строки .Select(long.Parse) // превратить строки в числа .Sum() // и всё сложить 
  • Tell me more, is it a button? Or is it better to create a separate class and make the button address this class? - komsonavt
  • @AlexanderTihonov: It depends on the size of the project. If the project is really small, and this is its main task, you can not bathe and do it in the click handler (yes, the antipattern). And if the project is a little more, then I would, of course, separate it into a separate class-model. - VladD
  • Tell me, how to assign the result of the sum variable? - komsonavt
  • @AlexanderTihonov: long sum = <тут всё выражение>; ? - VladD
  • @VladV that is, for output to the screen, I need to write MessageBox.Show (sum); ? - komsonavt