Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

This piece of code gets an error that those variables haven't been assgined to, and I can't figure out why

I want to make a static List, but when I try read from file, and give the lists a value, it says that it haven’t been assigned to, so its value is null.

class Log
    {   static public List<string> varos;
        static public List<int> tav;
        static public List<int> n;

        public void Input()
        {
            var sr = new StreamReader("vartav.txt");

            while (!sr.EndOfStream)
            {
                string s = sr.ReadLine();
                string[] seged = s.Split(' ');
                Log.varos.Add(seged[0]);
                Log.tav.Add(Convert.ToInt32(seged[1]));
            }
        } 

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A variable is not an object, it is simply a placeholder for an object. You are declaring the variables:

static public List<string> varos;

But you never initialized them to an object:

static public List<string> varos = new List<string>();

You could alternatively initialize them inside the method. Though of course they wouldn’t be initialized until that method is invoked.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading