-3

I created an array of datatype 'Persoon'. I'm trying to get the method 'ReadPerson()' to return 3 objects of 'Persoon'. By returning my array 'personen' I get the error "Cannot convert type 'Persoon[]' to 'Persoon'. I'm quite new to programming so I'm still unsure how to properly use arrays and structs. I tried return personen[i], but 'i' will of course be undefined outside the for-loop. How do I return the 3 objects of 'Persoon'?

    Persoon ReadPerson()
    {
        Persoon[] personen = new Persoon[3];

        for (int i = 0; i < personen.Length; i++)
        {
            personen[i].FirstName = LeesString("Enter first name: ");
            personen[i].SecondName = LeesString("Enter second name: ");
            personen[i].Residence = LeesString("Enter residence: ");
            personen[i].Age = LeesInt("Enter age: ", 0, 120);
            personen[i].Gender = LeesGeslacht("Enter gender (m/f): ");
            Console.Write("\n");
        }
        return personen[i];
    }

    void PrintPerson(Persoon p)
    {
        Console.Write("\n");

        Console.Write(p.FirstName + " " + p.SecondName + " ");
        PrintGeslacht(p.Gender);

        Console.Write("\n");
        Console.WriteLine("{0} jaar, {1}", p.Age, p.Residence);
    }

struct Persoon
{
    public string FirstName;
    public string SecondName;
    public string Residence;
    public int Age;
    public GeslachtType Gender;
}
  • You need to put `return` outside the loop. Anyways, having it like that, will cause your method to end on a first iteration. – Michał Turczyn Dec 12 '19 at 10:13
  • No. Please read carefully. – Leroy de Smet Dec 12 '19 at 10:16
  • 1
    _"I'm trying to get this method to return each of the 3 integers."_ then writing `return personen[i];` that's not the way to do that. – SᴇM Dec 12 '19 at 10:17
  • Your question has already been answered but I would like to give some advice. I see that you're programming in Dutch, I advice against that. I suggest to code everything in English as the framework itself is also in English. This way you got Dutch and English combines which would be a no-go in production. – EpicKip Dec 12 '19 at 10:17
  • The compiler isn´t that smart to determine that your loop is guaranteed to run at least once. The compiler is more pessimistic here. – MakePeaceGreatAgain Dec 12 '19 at 10:17
  • If you trying to print all persoonen items, you can use yield return instead. As far as I can see, this code part will only work for the first item of the personen array. Is this approach intentional?? – ycansener Dec 12 '19 at 10:18
  • Your edit turned your error in something completely new, namely from "Not all code paths return a value" to "Cannot convert `Persoon` to `Persoon[]`" (or similar). – MakePeaceGreatAgain Dec 12 '19 at 10:19
  • I'm still very new to this. No need to be rude, instead refer me to something that can actually help me. I've read the basics, I just simply don't understand. – Leroy de Smet Dec 12 '19 at 10:27
  • 1
    *I'm trying to get this method to return EACH of the 3 integers.* - that doesn't make sense. Are you trying to return three `Persoon` objects? If so, then @dunnel123's answer is the one you're looking for. – germi Dec 12 '19 at 10:31
  • Yes that is what I'm trying to do. Sorry for my poor wording. English isn't my strongest suit. Anyways, changing the method from Persoon to Persoon[] will result in tons of errors in my entire code. I'll just paste the whole thing in there now. – Leroy de Smet Dec 12 '19 at 10:33

1 Answers1

4

As it's currently written your for loop will only execute once and then return the first person.

You're getting the "Not all code paths return a value" error because that's exactly what's going on, the path of your method ends without a return but the method signature requires a return type.

It looks like you want something like this -

void Main()
{
    var people = LeesPersoon();

    for (var i = 0; i < people.Length; i++)
    {
        PrintPerson(people[i]);
    }
}

void PrintPerson(Persoon p)
{
    Console.Write("\n");

    Console.Write(p.FirstName + " " + p.SecondName + " ");
    PrintGeslacht(p.Gender);

    Console.Write("\n");
    Console.WriteLine("{0} jaar, {1}", p.Age, p.Residence);
}

Persoon[] LeesPersoon()
{
     Persoon[] personen = new Persoon[3];

     for (int i = 0; i < personen.Length; i++)
     {
          personen[i].FirstName = LeesString("Enter first name: ");
          personen[i].SecondName = LeesString("Enter second name: ");
          personen[i].Residence = LeesString("Enter residence: ");
          personen[i].Age = LeesInt("Enter age: ", 0, 120);
          personen[i].Gender = LeesGeslacht("Enter gender (m/f): ");
          //Console.Write("\n");
     }

     return personen;
}
dunnel123
  • 384
  • 1
  • 8