The object of your affection

Hi There

Can anyone help with number 9 exercise here;
https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-dating-profile

Been scratching my head a wee while now, I can’t figure out how in the SetHobbies method I assign the class field value string hobbies the values from the string array being passed into on the method.
As its an array I thought to loop through using for loop and increment each element of this.hobbies[i] = hobbies[i], however I’m getting an error that the fields are not being initialised.
I tried to copy the array from passed in parameter hobbies to this.hobbies with no luck either.
I’ve checked the incoming supplied array, it has 3 values in it, and with a foreach I can write output to console so know the values are in the incoming array - but for the life of me I haven;t had luck assigning the actual field members.

Any tips, I’m sure I’m missing something basic here!
Any help appreciated.

Hello, @nzmiket. Welcome to the forum!

I suspect you have a line in your Profile class that looks like this: private string[] hobbies;
From what you described, you can simply assign the array passed in to the empty hobbies array since they are each already arrays. Like so:

    public void SetHobbies(string[] hobbies)
    {
      this.hobbies = hobbies;
    }

If you are trying to loop through the parameter, and assign each value to this.hobbies[index] you will throw an error since the hobbies array is empty, and has no indexed positions for you to assign values to.

2 Likes

Thanks midlinder, that was exactly it!
Boy was I trying to over complicate it in hindsight.

Cheers again.

1 Like