Object of Your Affection step 12

So I’m getting this error and I’m not sure how to fix it. I am also unsure if i’m supposed to use an if statement to append hobbies. Or is there something I’m missing?

Profile.cs(16,124): error CS0246: The type or namespace name 'h
obbies' could not be found (are you missing a using directive o
r an assembly reference?) [/home/ccuser/workspace/csharp-dating
-profile/DatingProfile.csproj]
Profile.cs(16,120): error CS1736: Default parameter value for '
hobbies' must be a compile-time constant [/home/ccuser/workspac
e/csharp-dating-profile/DatingProfile.csproj]
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", string[] hobbies = new hobbies[0])
   {
    this.name = name; 
    this.age = age ; 
    this.city = city; 
    this.country = country; 
    this.pronouns = pronouns;
    this.hobbies = hobbies;
   }
    
  //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;
   }
    
    
  }
}

Alright I fixed one of the errors but I’m still being told to make hobbies a compile time constant.
Should I know what a compile- time constant is? I don’t remember reading about it at any point.
Anyways made progress.

Profile.cs(16,120): error CS1736: Default parameter val
ue for 'hobbies' must be a compile-time constant [/home
/ccuser/workspace/csharp-dating-profile/DatingProfile.c
sproj]
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", string[] hobbies = new string[0])
   {
    this.name = name; 
    this.age = age ; 
    this.city = city; 
    this.country = country; 
    this.pronouns = pronouns;
    this.hobbies = hobbies;
   }
    
  //methods
   public string ViewProfile()
   {
     
     string currentHobbies = " ";
     
     if (hobbies.Length > 0) 
     {
       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;
   }
    
    
  }
}

Hello, @barnowl121.

As I recall, from the project, we don’t provide a hobbies argument when we instantiate a Profile object. We use the setHobbies method to add hobbies later. Therefore, your constructor method should not have a parameter for hobbies.

1 Like

Thanks, @midlindner.

Once again you’ve been very helpful, it always seems like it’s the small or simple mistakes that really stump a person.

1 Like

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