reading text file with streamreader in object

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
71
Programming Experience
Beginner
I try to read a text file in an object but I get error message that I don't understand please help me. I need to do this homework.
the error msg is
Severity Code Description Project File Line Suppression State
Error CS1955 Non-invocable member 'StreamReader' cannot be used like a method. metjelentes C:\Users\Dell\source\repos\metjelentes\metjelentes\Program.cs 21 Active
the error appears at the line using(...


monthly weathercast:
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace metjelentes
{
    class adatok
    {
        public string telepules = " ";
        public string ido = " ";
        public string szeliranyes_erosseg = " ";
        public int homerseklet = 0;
    }
    class beolvasas
    {       
        public void beolvas()
         {
            string olvas = @"c:\Users\Public\textfiles\tavirathu13.txt";
            using (StreamReader sr = StreamReader(olvas, Encoding.Default))
                {
                    //StreamReader sr = new StreamReader(olvas, Encoding.Default);
                    List<adatok> lista = new List<adatok>();
          
                    while (!sr.EndOfStream)
                        {
                            string sor = sr.ReadLine();
                            string[] elemek = sor.Split(' ');
                            lista.Add(new adatok());

                        }
                }
          }
     }
        class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
 
It would help you tell us what error you are getting now.

Off the top of my head, it looks like you are creating a new instance of adatlista for each line that you find in your file. Probably not what you want to do. You probably want to create one instance and then keep adding to the list in that instance.
 
It would help you tell us what error you are getting now.

Off the top of my head, it looks like you are creating a new instance of adatlista for each line that you find in your file. Probably not what you want to do. You probably want to create one instance and then keep adding to the list in that instance.
thank you for help. the idea is to have record class and record list class separate to access it from different objects. one object is olvas( read) where I read data from text file, another would be another object with other task. so I think an instance would be the way to access the record list from different objects . obviously have to be filled up from olvas( read) to be useable for farther process. thank you
weather cast:
while (!sr.EndOfStream)
                        {
                            string sor = sr.ReadLine();
                            string[] elemek = sor.Split(' ');
                            adatlista adat = new adatlista();
                            adat.lista.Add(new adatok());
                            adat.lista[db].telepules=elemek[0];
                            adat.lista[db].ido = elemek[1];
                            adat.lista[db].szeliranyes_erosseg = elemek[2];
                            adat.lista[db].homerseklet = Int32.Parse(elemek[3]);
                            db++;
                        }
the error
(1,1): error CS0246: The type or namespace name 'adatlista' could not be found (are you missing a using directive or an assembly reference?)
(2,29): error CS0246: The type or namespace name 'adatlista' could not be found (are you missing a using directive or an assembly reference?)
(2,47): error CS0246: The type or namespace name 'adatok' could not be found (are you missing a using directive or an assembly reference?)
 
also I need to access the record list class so the created instance filled up previously in olvas(read) class
class felhasznalo
{
adatlista adat = new adatlista();
foreach (string elem in adat.lista)
{

}
}
 
If you need the list for later on, then why do you keep on creating a brand new object that holds the list on line 5 of your code in post #17. It's still the same problem as I described in post #16.

As for the errors you are getting, please post your complete code. The first error you are getting is referring to line 1, but it doesn't make sense since based on the code you presented in post #13, line one is using System; would have not even have the identifier adatlista for the compiler to complain about. Then the following two errors don't quite make sense since according to pest #13, you declared those two classes within the same namespace, yet the compiler can't find them. The only thing I can think of is that you are not showing us your complete code, nor the code within the context that it lives within.
 
If you need the list for later on, then why do you keep on creating a brand new object that holds the list on line 5 of your code in post #17. It's still the same problem as I described in post #16.

As for the errors you are getting, please post your complete code. The first error you are getting is referring to line 1, but it doesn't make sense since based on the code you presented in post #13, line one is using System; would have not even have the identifier adatlista for the compiler to complain about. Then the following two errors don't quite make sense since according to pest #13, you declared those two classes within the same namespace, yet the compiler can't find them. The only thing I can think of is that you are not showing us your complete code, nor the code within the context that it lives within.
thank you for help. my code is the following
weather cast:
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace metjelentes
{
    class adatok
    {
        public string telepules = " ";
        public string ido = " ";
        public string szeliranyes_erosseg = " ";
        public int homerseklet = 0;
    }
    class adatlista
    {
        public List<adatok> lista = new List<adatok>();
    }

    class beolvasas
     {      
        public void beolvas()
         {
            string olvas = @"c:\Users\Public\textfiles\tavirathu13.txt";
            using (StreamReader sr =new StreamReader(olvas, Encoding.Default))
                {
                //StreamReader sr = new StreamReader(olvas, Encoding.Default);

                //List<adatok> lista = new List<adatok>();
                int db = 0;
                    while (!sr.EndOfStream)
                        {
                            string sor = sr.ReadLine();
                            string[] elemek = sor.Split(' ');
                            adatlista adat = new adatlista();
                            adat.lista.Add(new adatok());
                            adat.lista[db].telepules=elemek[0];
                            adat.lista[db].ido = elemek[1];
                            adat.lista[db].szeliranyes_erosseg = elemek[2];
                            adat.lista[db].homerseklet = Int32.Parse(elemek[3]);
                            db++;
                        }
                Random rd = new Random();
                int rand_num = rd.Next(1, db);
            }
          }
     }
    /* */
    class felhasznalo
    {
        //adatlista adat = new adatlista();
        foreach (string[] elem in adatlista)
        {
         
        }
    }
       class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
            }
        }
}
obviously I don't want to create all the time new instances but use the created and filled up one already, thank you.
 
Last edited:

Latest posts

Back
Top Bottom