The Object Of You Affection Step 12

I’m having trouble validating the dating profile to people only 18 years old or older. I manage to do it to the ‘sam object’ only in main. But I know you are suppose to do it in the class somehow.

Here is what I got so far.

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;

    //Properties
  private string Name
  {get; set;}
  public int Age
  {
   get {return age;}
   set
  {
    if(age >= 18)
    {
      age = value;
    }
    else
    {
    Console.WriteLine("You are under age, please go away");
    }
  } 
  }
  private string City
  {get; set;}
  private string Country
  {get; set;}
  private string Pronouns
  {get; set;}
  private string[] Hobbies
  {get; set;}



    //Constructors
    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;
      this.hobbies = new string[0];
    }

    //Methods
   public string ViewProfile()
   {
     string bio = $"Name: {name}\nAge: {age}\nCity: {city}\nCountry: {country}\nPronouns: {pronouns}";



if(hobbies.Length > 0)
{
     bio += $"\nHobbies:\n";
foreach(string hobby in hobbies)
{
 bio += $"- {hobby}\n";
}
     return bio;
}
else
{
return bio;
}

     
   }

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

  }
}

As you can see I think you are suppose to do the if statement in the Age property but this way doesn’t work.

Hi,

To work you need change

public int Age
{
get {return age;}
set
{
if(value>= 18) //CHANGE
{
age = value;
}
else
{
Console.WriteLine(“You are under age, please go away”);
}
}

And
//Constructors
public Profile(string name, int age, string city, string country, string pronouns = “They/Them”)
{
this.name = name;
Age = age; //CHANGE
this.city = city;
this.country = country;
this.pronouns = pronouns;
this.hobbies = new string[0];
}