Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The out parameter must be assigned to before control leaves the current methods

Tags:

c#

I'm very new C# and this is the first time I'm doing anything with a list, so this might be a very dumb question... I'm trying to read data from a file to a list that consists of Tourist objects. As I understand I need to assign something to tourists list before I add objects to it, but I'm not sure how to do that.

class Tourist
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double Contributed { get; set; }

    public Tourist(string firstName, string lastName, double money)
    {
        FirstName = firstName;
        LastName = lastName;
        Contributed = money * 0.25;
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Tourist> tourists = new List<Tourist>();

        ReadData(out tourists);
    }

    static void ReadData(out List<Tourist> tourists)
    {
        const string Input = "..\\..\\Duomenys.txt";

        string[] lines = File.ReadAllLines(Input);
        foreach (string line in lines)
        {
            string[] values = line.Split(';');
            string firstName = values[0];
            string lastName = values[1];
            double money = Double.Parse(values[2]);
            tourists.Add(new Tourist(firstName, lastName, money));
        }
    }
}
like image 200
PoVa Avatar asked Feb 07 '23 03:02

PoVa


1 Answers

By declaring a parameter as out you "promise" the caller (and the compiler) that your method will set a value to the variable provided as argument for that parameter.

Because you promise it, every path through your method must assign a value to this parameter.

Your method does not assign a value to tourists. This may actually lead to a NullReferenceException at tourists.Add(...) if the method gets called with a null reference.

To me it seems you can ommit the out keyword as you initialize tourists already in Main. Note that ReadData only modifies the content of the list, not the reference to it stored in the tourists variable. Since you don't want to change that reference (the variable's value), you don't need the out keyword.

If you want ReadData to initialize it, you need to add the line

tourists = new List<Tourist>()

in ReadData before the foreach loop.


As your code stands, the better solution is to ommit any parameter to ReadData and let the method return the list instead:

static List<Tourist> ReadData()
{
    // create list
    List<Tourist> tourists = new List<Tourist>();

    const string Input = "..\\..\\Duomenys.txt";       

    string[] lines = File.ReadAllLines(Input);
    foreach (string line in lines)
    {
        // shortened for brevity
        tourists.Add(new Tourist(firstName, lastName, money));
    }

    return tourists; // return newly created list
}

And use this in Main like:

static void Main(string[] args)
{
    List<Tourist> tourists = ReadData();
}
like image 194
René Vogt Avatar answered May 24 '23 20:05

René Vogt