The Object of Your Affections step 10

What do I use inside the body of my loop? I have been trying to figure it out but haven’t had much luck.
Any help would be appreciated.

using System;

namespace DatingProfile
{ 
  class Profile 
  {
    // fields
   private string name;
   private int age;
   private string city;
   private string country;
   private string pronouns;
   private string[] hobbies;
    
   // constructor
   public Profile(string name, int age, string city, string country, string pronouns = "they/them")
   {
    this.name = name; 
    this.age = age ; 
    this.city = city; 
    this.country = country; 
    this.pronouns = pronouns;
   }
  //methods
   public string ViewProfile()
   {
       
     string bio = $"Name: {name}\n Age: {age}\n City: {city}\n Country: {country}\n Pronouns: {pronouns}\n Hobbies: {hobbies}";
     
     foreach(string hobbie in this.hobbies)
     {
       
     }
     
     return bio;
   }
   
   public void SetHobbies(string[] hobbies)
   {
     this.hobbies = hobbies;
   }
    
    
  }
}

Hello, @barnowl121.

Welcome to the forums.

In your ViewProfile() method it appears you are building a string, assigned to the variable bio, that will contain all of the information from an instance of Profile. Since hobbies is stored as an array, it looks like the purpose of your foreach loop is to add each element of the array to your string, bio. You may want to consider building a string of the hobbies. You could name it anything you like. Possibly something like currentHobbies. Then you could iterate through the hobbies array, and add each hobby to currentHobbies using simple concatenation: currentHobbies += hobby. If you did that first, you could then use {currentHobbies} in your bio instead of {hobbies}.

Thanks for the help. I made the corrections you suggested but I’m getting an error.

namespace DatingProfile
{ 
  class Profile 
  {
    // fields
   private string name;
   private int age;
   private string city;
   private string country;
   private string pronouns;
   private string[] hobbies;
    
   // constructor
   public Profile(string name, int age, string city, string country, string pronouns = "they/them")
   {
    this.name = name; 
    this.age = age ; 
    this.city = city; 
    this.country = country; 
    this.pronouns = pronouns;
   }
  //methods
   public string ViewProfile()
   {
    string currentHobbies = "listening to audiobooks and podcasts" + "playing rec sports like bowling and kickball" + "writing a speculative fiction novel" + "reading advice columns";
       
    foreach(string hobby in hobbies)
     {
       currentHobbies += hobby;
     } 

     string bio = $"Name: {name}\n Age: {age}\n City: {city}\n Country: {country}\n Pronouns: {pronouns}\n Hobbies: {currentHobbies}";
     
     return bio;
   }
   
   public void SetHobbies(string[] hobbies)
   {
     this.hobbies = hobbies;
   }
    
    
  }
}

//Error

Unhandled Exception: System.NullReferenceException: Object ref
erence not set to an instance of an object.
at DatingProfile.Profile.ViewProfile() in /home/ccuser/work
space/csharp-dating-profile/Profile.cs:line 29
at DatingProfile.Program.Main(String args) in /home/ccuse
r/workspace/csharp-dating-profile/Program.cs:line 12

I ran your code in place of my own, and did not see an error. You may have noticed in your posts that I formatted your code. Please review these guidelines for future posts: How do I format code in my posts?

You shouldn’t need to include hobbies in this line:

string currentHobbies = "listening to audiobooks and podcasts" + "playing rec sports like bowling and kickball" + "writing a speculative fiction novel" + "reading advice columns";

You already have a SetHobbies method. Just declare the variable, and initialize it as an empty string:
string currentHobbies = "";

Also, where you concatenate the hobbies to currentHobbies, you may want to add a comma and a space as well: currentHobbies += hobby + ", ";.

These are not the source of your error. Could you post your code from Program.cs?

Well it appears that these latest changes have resolved my error. So thanks again for the feed back. I’m just gonna post the rest of my code for posterity.

using System;

namespace DatingProfile
{ 
  class Profile 
  {
    // fields
   private string name;
   private int age;
   private string city;
   private string country;
   private string pronouns;
   private string[] hobbies;
    
   // constructor
   public Profile(string name, int age, string city, string country, string pronouns = "they/them")
   {
    this.name = name; 
    this.age = age ; 
    this.city = city; 
    this.country = country; 
    this.pronouns = pronouns;
   }
  //methods
   public string ViewProfile()
   {
     
     string currentHobbies = " ";
       
    foreach(string hobby in hobbies)
     {
       currentHobbies += hobby + ",";
     } 

     string bio = $"Name: {name}\n Age: {age}\n City: {city}\n Country: {country}\n Pronouns: {pronouns}\n Hobbies: {currentHobbies}";
     
     return bio;
   }
   
   public void SetHobbies(string[] hobbies)
   {
     this.hobbies = hobbies;
   }
    
    
  }
}
using System;

namespace DatingProfile
{
  class Program
  {
    static void Main(string[] args)
    {
      Profile sam = new Profile("Sam Drakkila", 30, "New York", "USA", "he/him");
      
      sam.SetHobbies(new string[] {"listening to audiobooks and podcasts" , "playing rec sports like bowling and kickball" , "writing a speculative fiction novel" , "reading advice columns"});
      
     Console.WriteLine(sam.ViewProfile());
      
    }
  }
}

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.