Resolved Assigning value to nested array

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
I'm having problem with the syntax of how to add an existing value to the index of an array in array.

Assigning values to the main array is no problem i.e.:
C#:
Pets[0].Name = "Brooom";

... but reaching the Toys array and its fields is a trickier one. I manage to get the field by iterating through the array using a foreach loop. But how isn't it possible to target an specifik index and its value like:
What's the syntax:
Pets[0].Toys[0].?...what goes here to get i.e. the color?

EDIT: I'm a bit embarressed, fresh eyes and I managed to solve it lol.
C#:
Pets[0].Toys[0].Name = "Brooom";
 
Last edited:
I'm glad you figured it out.

Since your teachers seem to be taking time to teach you about object oriented class design, I would remind you of The Law of Demeter. Perhaps your teachers haven't gotten to it yet, or maybe their current focus right now is to teach you about arrays, but it's something to keep in mind as you go through your programming career.
 
Back
Top Bottom