0

I want to implement an interface that's quite simple :

public interface IProfession
{
    List<string> jobSkills { get; set; }
    void setSkills();
}


public class artisan : IProfession
{
    int money = 1200;

    public List<string> jobSkills;

    public void setSkills(){
        jobSkills.Add("baratin");
        jobSkills.Add("marchandage");
        jobSkills.Add("monde_naturel");
        jobSkills.Add("royaume_natal");
        jobSkills.Add("sagacite");
        jobSkills.Add("statut");
        jobSkills.Add("statut");
    }
}

Vstudio tells me that Models.artisan does not implement interface member Models.IProfession.jobSkills.

Why? I have tried this too :

public class artisan : IProfession
{
    int money = 1200;

    protected List<string> jobSkills;

    public List<string> _jobSkills
{ 
    get { return jobSkills;}
    set { jobSkills = value; }
} 

    public void setSkills(){
        jobSkills.Add("baratin");
        jobSkills.Add("marchandage");
        jobSkills.Add("monde_naturel");
        jobSkills.Add("royaume_natal");
        jobSkills.Add("sagacite");
        jobSkills.Add("statut");
        jobSkills.Add("statut");
    }
}

but it does not work either (same error). can someone explain what I'm missing? Thank you

DoctorPrisme
  • 103
  • 2
  • 11
  • Very tempted to close this as a dupe of http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c – DavidG Dec 08 '16 at 12:51

2 Answers2

2

Your interface requires a property jobSkills. You will need to implement a property then:

public interface IProfession
{
    List<string> JobSkills { get; set; }
    void SetSkills();
}    

public class Artisan : IProfession
{
    public List<string> JobSkills { get; set; };

    public void SetSkills()
    {
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Gratz. Got to re-read the basics. Thank you, I'll validate this in a few minutes. Would you mind telling me how the line I wrote is named if it's not a property though? – DoctorPrisme Dec 08 '16 at 12:55
  • 1
    @DoctorPrisme Read the link I posted above, that will explain the difference. – DavidG Dec 08 '16 at 12:56
  • 1
    @DavidG : well, it was indeed the same source of problem but I had no idea of it, as the message of VS wasn't empathetic on that point; and linking to that question wouldn't have helped me. However your link is useful. Thank you. – DoctorPrisme Dec 08 '16 at 12:58
  • @DoctorPrisme That's the only reason I didn't hammer it closed, Though there may be a better question/answer that is a real dupe – DavidG Dec 08 '16 at 12:59
1

You need

public List<string> jobSkills { get; set; }

Also in your method you need to initialize the list.

public void setSkills()
{
    if(jobSkills == null)
       jobSkills = new List<string>();

    jobSkills.Add("baratin");
    jobSkills.Add("marchandage");
    jobSkills.Add("monde_naturel");
    jobSkills.Add("royaume_natal");
    jobSkills.Add("sagacite");
    jobSkills.Add("statut");
    jobSkills.Add("statut");
}
mybirthname
  • 17,949
  • 3
  • 31
  • 55