-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;
}
10
  • You need to put return outside the loop. Anyways, having it like that, will cause your method to end on a first iteration. Commented Dec 12, 2019 at 10:13
  • No. Please read carefully. Commented Dec 12, 2019 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. Commented Dec 12, 2019 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. Commented Dec 12, 2019 at 10:17
  • 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. Commented Dec 12, 2019 at 10:31

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

I get the error 'cannot convert type 'Persoon[]' to 'Persoon' at the method PrintPerson(ReadPerson()); I edited the original message so you can see the rest of the code
Store the return value of LeesPersoon into a variable - var people = LeesPersoon() then you can loop through people and print the values in the loop. You would access each person like - people[0].Voornaam etc...
What do you mean store the return value into a variable? Which return value would that be?
The people array returned from ReadPerson()
I'm sorry I don't get it, what do I write as return value?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.